-2

このクエリがグループ化で照合エラーを生成する理由を理解するのに苦労しています。特定のケースステートメントに絞り込みました。それは一方向に(結果を文字列として)動作しますが、別のテーブルのフィールドを置き換えようとすると(それ自体で問題なく表示されます)、照合エラーがスローされます。

これが正常に動作するコードです。

Declare @StartDate Date = '09/01/2013';
Declare @EndDate Date = Getdate();

With Invoice as (
        SELECT     
         a.DocEntry
        ,a.DocDate
        ,a.CardCode
        ,a.CardName
        ,a.U_CXS_FMST
        ,a.U_CXS_FRST
        ,a.U_DBS1StoreID
        ,CASE
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = 3 then 2
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = 4 then 3
            when a.DocDate >= '08/01/2011' then isnull(U_CXS_FMST,U_DBS1StoreID)
        end as CompleteStores
        ,d.description StoreName
        ,CASE a.U_CXS_FRST
                    WHEN N'Y' THEN 1
                    WHEN N'N' THEN 0
                    ELSE 0
              END [iVend]
        ,     CASE 
                    WHEN ISNULL(a.U_DBS1StoreID,0)=0 THEN 0
                    WHEN ISNULL(a.U_DBS1StoreID,0)<>0 THEN 1
              END   [DBS]
        ,     CASE a.CardCode
                    WHEN N'C100' THEN 1
                    ELSE 0
              END [Web]
        ,a.NumAtCard
        ,a.U_TransactionID
        ,a.U_SalesSource
        ,s.Name
        ,b.LineNum
        ,b.ItemCode
        ,c.ItemName
        ,c.CstGrpCode
        ,c.U_WebName
        ,c.U_SupplierCatNum
        ,c.CatCode
        ,c.CatName
        ,c.GroupCode
        ,c.GroupName
        ,c.DeptCode
        ,c.DeptName
        ,c.MfcCode
        ,c.Manufacturer
        ,Cast(b.Quantity as INT) Quantity
        ,b.Price
        ,b.PriceBefDi
        ,b.LineTotal
        ,b.GrossBuyPr
        ,a.DocTotal
        --,((Cast(b.Quantity as INT)* b.Price)-(Cast(b.Quantity as INT)*b.GrossBuyPr))/b.GrossBuyPr as MarginPCT

        FROM         
        MonkeySports.dbo.INV1 AS b 
        INNER JOIN 
        MonkeySports.dbo.OINV AS a ON a.DocEntry = b.DocEntry
        Inner Join
        dbo.MS_OITM_Categories as c ON b.ItemCode = c.ItemCode
        Inner Join
        [@SALESSOURCE] as s on a.U_SalesSource = s.Code
        left outer join
        CXSRetail.dbo.RtlStore d on 
        CASE
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = '3' then '2'
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = '4' then '3'
            when a.DocDate >= '08/01/2011' then isnull(U_CXS_FMST,U_DBS1StoreID)
        end = cast(d.siteid as Nvarchar)

        Where 
GrossBuyPr > 0.01
and
a.DocDate between @StartDate and @EndDate
) 
,Invoice1 as (
        Select 
         DocEntry
        ,DocDate
        ,CardCode
        ,CardName
        ,U_CXS_FMST
        ,U_CXS_FRST
        ,U_DBS1StoreID
        ,CompleteStores
        ,StoreName
        ,CASE U_CXS_FRST
                    WHEN N'Y' THEN 1
                    WHEN N'N' THEN 0
                    ELSE 0
              END [iVend]
        ,     CASE 
                    WHEN ISNULL(U_DBS1StoreID,0)=0 THEN 0
                    WHEN ISNULL(U_DBS1StoreID,0)<>0 THEN 1
              END   [DBS]
        ,     CASE CardCode
                    WHEN N'C100' THEN 1
                    ELSE 0
              END [Web]
                    ,CASE
                WHEN iVend = 0 AND DBS = 0 AND Web = 0
                THEN 1
                ELSE 0
            END [Other]   
        ,NumAtCard
        ,U_TransactionID
        ,U_SalesSource
        ,Name
        ,LineNum
        ,ItemCode
        ,ItemName
        ,CstGrpCode
        ,U_WebName
        ,U_SupplierCatNum
        ,CatCode
        ,CatName
        ,GroupCode
        ,GroupName
        ,DeptCode
        ,DeptName
        ,MfcCode
        ,Manufacturer
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
        ,DocTotal
        --,MarginPCT

        From Invoice    
        )
,Invoice2 as (      
    SELECT
        --DocEntry
         --DocDate
        --,CardCode
        --,CardName
        --,U_CXS_FMST
        --,U_CXS_FRST
        --,U_DBS1StoreID
        --,CompleteStores
          CASE
            When iVend = 1 or DBS = 1 then 'Retail'
            When Web = 1 then 'Web'
            When Other = 1 then 'Other'
          End SalesChnl   
          ,Case 
            When iVend = 1 OR DBS = 1 Then StoreName
            When Web = 1 Then 'Internet'
            When Other = 1 then 'Other' 
         End StoreName
        ,iVend
        ,DBS
        ,Web
        ,Other  
        --,NumAtCard
        --,U_TransactionID
        ,U_SalesSource
        ,Name
        --,LineNum
        --,CstGrpCode
        --,U_WebName
        --,U_SupplierCatNum
        --,CatCode
        ,CatName
        --,GroupCode
        ,GroupName
        --,DeptCode
        ,DeptName
        --,MfcCode
        ,Manufacturer
        ,ItemCode
        ,ItemName
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
        --,MarginPCT
        --,DocTotal         
FROM Invoice1   

--Where GrossBuyPr = 0

Group By
        --DocEntry
        --DocDate
        --,CardCode
        --,CardName
        --,U_CXS_FMST
        --,U_CXS_FRST
        --,U_DBS1StoreID
        --,CompleteStores
        CASE
            When iVend = 1 or DBS = 1 then 'Retail'
            When Web = 1 then 'Web'
            When Other = 1 then 'Other'
          End    
        ,Case 
            When iVend = 1 OR DBS = 1 Then StoreName
            When Web = 1 Then 'Internet'
            When Other = 1 then 'Other'
         End
        ,iVend
        ,DBS
        ,Web
        ,Other  
        --,NumAtCard
        --,U_TransactionID
        ,U_SalesSource
        ,Name
        --,LineNum
        --,CstGrpCode
        --,U_WebName
        --,U_SupplierCatNum
        --,CatCode
        ,CatName
        --,GroupCode
        ,GroupName
        --,DeptCode
        ,DeptName
        --,MfcCode
        ,Manufacturer
        ,ItemCode
        ,ItemName
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
        --,MarginPCT
        --,DocTotal   
        )
Select *
 from Invoice2

しかし、このコードを試すと、照合エラーがスローされます。

    Declare @StartDate Date = '09/01/2013';
Declare @EndDate Date = Getdate();

With Invoice as (
        SELECT     
         a.DocEntry
        ,a.DocDate
        ,a.CardCode
        ,a.CardName
        ,a.U_CXS_FMST
        ,a.U_CXS_FRST
        ,a.U_DBS1StoreID
        ,CASE
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = 3 then 2
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = 4 then 3
            when a.DocDate >= '08/01/2011' then isnull(U_CXS_FMST,U_DBS1StoreID)
        end as CompleteStores
        ,d.description StoreName
        ,CASE a.U_CXS_FRST
                    WHEN N'Y' THEN 1
                    WHEN N'N' THEN 0
                    ELSE 0
              END [iVend]
        ,     CASE 
                    WHEN ISNULL(a.U_DBS1StoreID,0)=0 THEN 0
                    WHEN ISNULL(a.U_DBS1StoreID,0)<>0 THEN 1
              END   [DBS]
        ,     CASE a.CardCode
                    WHEN N'C100' THEN 1
                    ELSE 0
              END [Web]
        ,a.NumAtCard
        ,a.U_TransactionID
        ,a.U_SalesSource
        ,s.Name
        ,b.LineNum
        ,b.ItemCode
        ,c.ItemName
        ,c.CstGrpCode
        ,c.U_WebName
        ,c.U_SupplierCatNum
        ,c.CatCode
        ,c.CatName
        ,c.GroupCode
        ,c.GroupName
        ,c.DeptCode
        ,c.DeptName
        ,c.MfcCode
        ,c.Manufacturer
        ,Cast(b.Quantity as INT) Quantity
        ,b.Price
        ,b.PriceBefDi
        ,b.LineTotal
        ,b.GrossBuyPr
        ,a.DocTotal
        --,((Cast(b.Quantity as INT)* b.Price)-(Cast(b.Quantity as INT)*b.GrossBuyPr))/b.GrossBuyPr as MarginPCT

        FROM         
        MonkeySports.dbo.INV1 AS b 
        INNER JOIN 
        MonkeySports.dbo.OINV AS a ON a.DocEntry = b.DocEntry
        Inner Join
        dbo.MS_OITM_Categories as c ON b.ItemCode = c.ItemCode
        Inner Join
        [@SALESSOURCE] as s on a.U_SalesSource = s.Code
        left outer join
        CXSRetail.dbo.RtlStore d on 
        CASE
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = '3' then '2'
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = '4' then '3'
            when a.DocDate >= '08/01/2011' then isnull(U_CXS_FMST,U_DBS1StoreID)
        end = cast(d.siteid as Nvarchar)

        Where 
GrossBuyPr > 0.01
and
a.DocDate between @StartDate and @EndDate
) 
,Invoice1 as (
        Select 
         DocEntry
        ,DocDate
        ,CardCode
        ,CardName
        ,U_CXS_FMST
        ,U_CXS_FRST
        ,U_DBS1StoreID
        ,CompleteStores
        ,StoreName
        ,CASE U_CXS_FRST
                    WHEN N'Y' THEN 1
                    WHEN N'N' THEN 0
                    ELSE 0
              END [iVend]
        ,     CASE 
                    WHEN ISNULL(U_DBS1StoreID,0)=0 THEN 0
                    WHEN ISNULL(U_DBS1StoreID,0)<>0 THEN 1
              END   [DBS]
        ,     CASE CardCode
                    WHEN N'C100' THEN 1
                    ELSE 0
              END [Web]
                    ,CASE
                WHEN iVend = 0 AND DBS = 0 AND Web = 0
                THEN 1
                ELSE 0
            END [Other]   
        ,NumAtCard
        ,U_TransactionID
        ,U_SalesSource
        ,Name
        ,LineNum
        ,ItemCode
        ,ItemName
        ,CstGrpCode
        ,U_WebName
        ,U_SupplierCatNum
        ,CatCode
        ,CatName
        ,GroupCode
        ,GroupName
        ,DeptCode
        ,DeptName
        ,MfcCode
        ,Manufacturer
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
        ,DocTotal
        --,MarginPCT

        From Invoice    
        )
,Invoice2 as (      
    SELECT
        --DocEntry
         --DocDate
        --,CardCode
        --,CardName
        --,U_CXS_FMST
        --,U_CXS_FRST
        --,U_DBS1StoreID
        --,CompleteStores
          CASE
            When iVend = 1 or DBS = 1 then 'Retail'
            When Web = 1 then 'Web'
            When Other = 1 then 'Other'
          End SalesChnl   
          ,Case 
            When iVend = 1 OR DBS = 1 Then StoreName
            When Web = 1 Then Name --'Internet'
            When Other = 1 then 'Other' 
         End StoreName
        ,iVend
        ,DBS
        ,Web
        ,Other  
        --,NumAtCard
        --,U_TransactionID
        ,U_SalesSource
        ,Name
        --,LineNum
        --,CstGrpCode
        --,U_WebName
        --,U_SupplierCatNum
        --,CatCode
        ,CatName
        --,GroupCode
        ,GroupName
        --,DeptCode
        ,DeptName
        --,MfcCode
        ,Manufacturer
        ,ItemCode
        ,ItemName
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
        --,MarginPCT
        --,DocTotal         
FROM Invoice1   

--Where GrossBuyPr = 0

Group By
        --DocEntry
        --DocDate
        --,CardCode
        --,CardName
        --,U_CXS_FMST
        --,U_CXS_FRST
        --,U_DBS1StoreID
        --,CompleteStores
        CASE
            When iVend = 1 or DBS = 1 then 'Retail'
            When Web = 1 then 'Web'
            When Other = 1 then 'Other'
          End    
        ,Case 
            When iVend = 1 OR DBS = 1 Then StoreName
            When Web = 1 Then Name --'Internet'
            When Other = 1 then 'Other'
         End
        ,iVend
        ,DBS
        ,Web
        ,Other  
        --,NumAtCard
        --,U_TransactionID
        ,U_SalesSource
        ,Name
        --,LineNum
        --,CstGrpCode
        --,U_WebName
        --,U_SupplierCatNum
        --,CatCode
        ,CatName
        --,GroupCode
        ,GroupName
        --,DeptCode
        ,DeptName
        --,MfcCode
        ,Manufacturer
        ,ItemCode
        ,ItemName
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
        --,MarginPCT
        --,DocTotal   
        )
Select *
 from Invoice2

唯一の違いは、文字列 Internet の代わりに @salessource テーブルの Name 列を使用しようとするこの case 式です。

,Case 
When iVend = 1 OR DBS = 1 Then StoreName
When Web = 1 Then Name --'Internet'
When Other = 1 then 'Other' 
 End StoreName

照合を強制しようとしましたが、効果はありませんでした。

どんな援助も大歓迎です。

4

2 に答える 2

0

したがって、最終的な解決策は、真の #temp テーブルを作成して、照合の問題がある 2 つのテーブルからのデータを格納し、そのテーブルをメイン クエリの CTE に結合することでした。

最終的なコードは次のようになります。

Declare @StartDate Date = '09/01/2013';
Declare @EndDate Date = Getdate();

Create Table #StoreTable (
SalesChnl Nvarchar(20),
StoreID Nvarchar(5),
StoreName Nvarchar(50));

insert into #StoreTable
SELECT 
'Web'
,[Code] 
,[Name]
FROM [@SALESSOURCE] 

insert into #StoreTable
Select   
'Retail'
,SiteId 
,Description
from CXSRetail.dbo.RtlStore;

With Invoice as (
        SELECT     
         a.DocEntry
        ,a.DocDate
        ,a.CardCode
        ,a.CardName
        ,a.U_CXS_FMST
        ,a.U_CXS_FRST
        ,a.U_DBS1StoreID
        ,CASE
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = 3 then 2
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = 4 then 3
            when a.DocDate >= '08/01/2011' then isnull(U_CXS_FMST,U_DBS1StoreID)
        end as CompleteStores
        ,CASE a.U_CXS_FRST
                    WHEN N'Y' THEN 1
                    WHEN N'N' THEN 0
                    ELSE 0
              END [iVend]
        ,     CASE 
                    WHEN ISNULL(a.U_DBS1StoreID,0)=0 THEN 0
                    WHEN ISNULL(a.U_DBS1StoreID,0)<>0 THEN 1
              END   [DBS]
        ,     CASE a.CardCode
                    WHEN N'C100' THEN 1
                    ELSE 0
              END [Web]
        ,a.NumAtCard
        ,a.U_TransactionID
        ,a.U_SalesSource
        ,b.LineNum
        ,b.ItemCode
        ,c.ItemName
        ,c.CstGrpCode
        ,c.U_WebName
        ,c.U_SupplierCatNum
        ,c.CatCode
        ,c.CatName
        ,c.GroupCode
        ,c.GroupName
        ,c.DeptCode
        ,c.DeptName
        ,c.MfcCode
        ,c.Manufacturer
        ,Cast(b.Quantity as INT) Quantity
        ,b.Price
        ,b.PriceBefDi
        ,b.LineTotal
        ,b.GrossBuyPr
        ,a.DocTotal
        ,((Cast(b.Quantity as INT)* b.Price)-(Cast(b.Quantity as INT)*b.GrossBuyPr))/(Cast(b.Quantity as INT)*b.GrossBuyPr)*100 as MarginPCT

        FROM         
        MonkeySports.dbo.INV1 AS b 
        INNER JOIN 
        MonkeySports.dbo.OINV AS a ON a.DocEntry = b.DocEntry
        Inner Join
        dbo.MS_OITM_Categories as c ON b.ItemCode = c.ItemCode
Where 
GrossBuyPr > 0.01
and
a.DocDate between @StartDate and @EndDate
) 
,Invoice1 as (
        Select 
         DocEntry
        ,DocDate
        ,CardCode
        ,CardName
        ,U_CXS_FMST
        ,U_CXS_FRST
        ,U_DBS1StoreID
        ,CompleteStores
        ,CASE U_CXS_FRST
                    WHEN N'Y' THEN 1
                    WHEN N'N' THEN 0
                    ELSE 0
              END [iVend]
        ,     CASE 
                    WHEN ISNULL(U_DBS1StoreID,0)=0 THEN 0
                    WHEN ISNULL(U_DBS1StoreID,0)<>0 THEN 1
              END   [DBS]
        ,     CASE CardCode
                    WHEN N'C100' THEN 1
                    ELSE 0
              END [Web]
                    ,CASE
                WHEN iVend = 0 AND DBS = 0 AND Web = 0
                THEN 1
                ELSE 0
            END [Other]   
        ,NumAtCard
        ,U_TransactionID
        ,U_SalesSource
        ,LineNum
        ,ItemCode
        ,ItemName
        ,CstGrpCode
        ,U_WebName
        ,U_SupplierCatNum
        ,CatCode
        ,CatName
        ,GroupCode
        ,GroupName
        ,DeptCode
        ,DeptName
        ,MfcCode
        ,Manufacturer
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
        ,DocTotal
        ,MarginPCT

        From Invoice    
        )
,Invoice2 as (      
    SELECT
        DocEntry
         --DocDate
        --,CardCode
        --,CardName
        --,U_CXS_FMST
        --,U_CXS_FRST
        --,U_DBS1StoreID
        --,CompleteStores
          ,CASE
            When iVend = 1 or DBS = 1 then 'Retail'
            When Web = 1 then 'Web'
            When Other = 1 then 'Other'
          End SalesChnl   
        ,CASE 
             When iVend = 1 OR DBS = 1 then CompleteStores
             When Web = 1 OR Other = 1 then U_SalesSource
         End as StoreID  
         ,#StoreTable.StoreName    
        --,iVend
        --,DBS
        --,Web
        --,Other    
        --,NumAtCard
        --,U_TransactionID
        --,U_SalesSource
        --,Name
        --,LineNum
        --,CstGrpCode
        --,U_WebName
        --,U_SupplierCatNum
        --,CatCode
        ,CatName
        --,GroupCode
        ,GroupName
        --,DeptCode
        ,DeptName
        --,MfcCode
        ,Manufacturer
        ,ItemCode
        ,ItemName
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
        --,DocTotal         
        ,MarginPCT
FROM Invoice1 
inner join
#StoreTable on
CASE
            When iVend = 1 or DBS = 1 then 'Retail'
            When Web = 1 then 'Web'
            When Other = 1 then 'Web'
          End  = #StoreTable.SalesChnl
AND           

CASE 
             When iVend = 1 OR DBS = 1 then CompleteStores
             When Web = 1 OR Other = 1 then U_SalesSource
         End = #StoreTable.StoreID

Group By
        DocEntry
        --DocDate
        --,CardCode
        --,CardName
        --,U_CXS_FMST
        --,U_CXS_FRST
        --,U_DBS1StoreID
        --,CompleteStores
        ,CASE
            When iVend = 1 or DBS = 1 then 'Retail'
            When Web = 1 then 'Web'
            When Other = 1 then 'Other'
          End    
         ,CASE 
             When iVend = 1 OR DBS = 1 then CompleteStores
             When Web = 1 OR Other = 1 then U_SalesSource
         End   
         ,#StoreTable.StoreName    
        --,iVend
        --,DBS
        --,Web
        --,Other    
        --,NumAtCard
        --,U_TransactionID
        --,U_SalesSource
        --,Name
        --,LineNum
        --,CstGrpCode
        --,U_WebName
        --,U_SupplierCatNum
        --,CatCode
        ,CatName
        --,GroupCode
        ,GroupName
        --,DeptCode
        ,DeptName
        --,MfcCode
        ,Manufacturer
        ,ItemCode
        ,ItemName
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
      --,DocTotal   
        ,MarginPCT
        )
,CreditMemo as (
        SELECT     
         a.DocEntry
        ,a.DocDate
        ,a.CardCode
        ,a.CardName
        ,a.U_CXS_FMST
        ,a.U_CXS_FRST
        ,a.U_DBS1StoreID
        ,CASE
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = 3 then 2
            when a.DocDate < '08/01/2011' and U_DBS1StoreID = 4 then 3
            when a.DocDate >= '08/01/2011' then isnull(U_CXS_FMST,U_DBS1StoreID)
        end as CompleteStores
        ,CASE a.U_CXS_FRST
                    WHEN N'Y' THEN 1
                    WHEN N'N' THEN 0
                    ELSE 0
              END [iVend]
        ,     CASE 
                    WHEN ISNULL(a.U_DBS1StoreID,0)=0 THEN 0
                    WHEN ISNULL(a.U_DBS1StoreID,0)<>0 THEN 1
              END   [DBS]
        ,     CASE a.CardCode
                    WHEN N'C100' THEN 1
                    ELSE 0
              END [Web]
        ,a.NumAtCard
        ,a.U_TransactionID
        ,a.U_SalesSource
        ,b.LineNum
        ,b.ItemCode
        ,c.ItemName
        ,c.CstGrpCode
        ,c.U_WebName
        ,c.U_SupplierCatNum
        ,c.CatCode
        ,c.CatName
        ,c.GroupCode
        ,c.GroupName
        ,c.DeptCode
        ,c.DeptName
        ,c.MfcCode
        ,c.Manufacturer
        ,(Cast(b.Quantity as INT)*-1) Quantity
        ,b.Price
        ,b.PriceBefDi
        ,b.LineTotal
        ,b.GrossBuyPr
        ,a.DocTotal
        ,((Cast(b.Quantity as INT)* b.Price)-(Cast(b.Quantity as INT)*b.GrossBuyPr))/(Cast(b.Quantity as INT)*b.GrossBuyPr)*100 as MarginPCT

        FROM         
        MonkeySports.dbo.RIN1 AS b 
        INNER JOIN 
        MonkeySports.dbo.ORIN AS a ON a.DocEntry = b.DocEntry
        Inner Join
        dbo.MS_OITM_Categories as c ON b.ItemCode = c.ItemCode
Where 
GrossBuyPr > 0.01
and
a.DocDate between @StartDate and @EndDate       
) 
,CreditMemo1 as (
        Select 
         DocEntry
        ,DocDate
        ,CardCode
        ,CardName
        ,U_CXS_FMST
        ,U_CXS_FRST
        ,U_DBS1StoreID
        ,CompleteStores
        ,CASE U_CXS_FRST
                    WHEN N'Y' THEN 1
                    WHEN N'N' THEN 0
                    ELSE 0
              END [iVend]
        ,     CASE 
                    WHEN ISNULL(U_DBS1StoreID,0)=0 THEN 0
                    WHEN ISNULL(U_DBS1StoreID,0)<>0 THEN 1
              END   [DBS]
        ,     CASE CardCode
                    WHEN N'C100' THEN 1
                    ELSE 0
              END [Web]
                    ,CASE
                WHEN iVend = 0 AND DBS = 0 AND Web = 0
                THEN 1
                ELSE 0
            END [Other]   
        ,NumAtCard
        ,U_TransactionID
        ,U_SalesSource
        ,LineNum
        ,ItemCode
        ,ItemName
        ,CstGrpCode
        ,U_WebName
        ,U_SupplierCatNum
        ,CatCode
        ,CatName
        ,GroupCode
        ,GroupName
        ,DeptCode
        ,DeptName
        ,MfcCode
        ,Manufacturer
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
        ,DocTotal
        ,MarginPCT

        From CreditMemo 
        )
,CreditMemo2 as (       
    SELECT
        DocEntry
         --DocDate
        --,CardCode
        --,CardName
        --,U_CXS_FMST
        --,U_CXS_FRST
        --,U_DBS1StoreID
        --,CompleteStores
          ,CASE
            When iVend = 1 or DBS = 1 then 'Retail'
            When Web = 1 then 'Web'
            When Other = 1 then 'Other'
          End SalesChnl   
         ,CASE 
             When iVend = 1 OR DBS = 1 then CompleteStores
             When Web = 1 OR Other = 1 then U_SalesSource
         End as StoreID  
         ,#StoreTable.StoreName  
        --,iVend
        --,DBS
        --,Web
        --,Other    
        --,NumAtCard
        --,U_TransactionID
        --,U_SalesSource
        --,Name
        --,LineNum
        --,CstGrpCode
        --,U_WebName
        --,U_SupplierCatNum
        --,CatCode
        ,CatName
        --,GroupCode
        ,GroupName
        --,DeptCode
        ,DeptName
        --,MfcCode
        ,Manufacturer
        ,ItemCode
        ,ItemName
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
        --,DocTotal         
        ,MarginPCT
FROM CreditMemo1
inner join
#StoreTable on
CASE
            When iVend = 1 or DBS = 1 then 'Retail'
            When Web = 1 then 'Web'
            When Other = 1 then 'Web'
          End  = #StoreTable.SalesChnl
AND           
CASE 
             When iVend = 1 OR DBS = 1 then CompleteStores
             When Web = 1 OR Other = 1 then U_SalesSource
         End = #StoreTable.StoreID
Group By
        DocEntry
        --DocDate
        --,CardCode
        --,CardName
        --,U_CXS_FMST
        --,U_CXS_FRST
        --,U_DBS1StoreID
        --,CompleteStores
        ,CASE
            When iVend = 1 or DBS = 1 then 'Retail'
            When Web = 1 then 'Web'
            When Other = 1 then 'Other'
          End    
        ,CASE 
             When iVend = 1 OR DBS = 1 then CompleteStores
             When Web = 1 OR Other = 1 then U_SalesSource
         End    
         ,#StoreTable.StoreName  
        --,iVend
        --,DBS
        --,Web
        --,Other    
        --,NumAtCard
        --,U_TransactionID
        --,Name
        --,U_SalesSource
        --,LineNum
        --,CstGrpCode
        --,U_WebName
        --,U_SupplierCatNum
        --,CatCode
        ,CatName
        --,GroupCode
        ,GroupName
        --,DeptCode
        ,DeptName
        --,MfcCode
        ,Manufacturer
        ,ItemCode
        ,ItemName
        ,Quantity
        ,Price
        ,PriceBefDi
        ,LineTotal
        ,GrossBuyPr
        --,DocTotal   
        ,MarginPCT
        )

Select *
from 
Invoice2  
Union 

Select *
from 
CreditMemo2  
Order by 
MarginPCT Desc
,SalesChnl
,StoreName
,CatName
,DeptName

DROP TABLE #StoreTable

助けてくれてありがとう。

于 2013-10-22T20:15:56.607 に答える