2013-10-21 26 views
-2

我很難找出爲什麼此查詢在分組中產生排序規則錯誤。 我把它縮小到一個特定的案例陳述。它的工作原理是一種方式(結果是一個字符串),但是當我嘗試從另一個表中替換一個字段時(它在你自己的頭腦中顯示得很好),它會引發排序錯誤。爲什麼此SQL服務器查詢產生排序規則衝突

這是可以正常工作的代碼。

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 

唯一的區別是這種情況下表達,我試圖從@salessource表而不是字符串互聯網使用的名稱列。

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

我試過強制排序,並沒有任何效果。

任何援助不勝感激。

+3

首先刪除儘可能多的東西,而不會丟失錯誤。 – Laurence

回答

0

因此,最終的解決方案是創建一個真正的#temp表來存儲來自兩個表中具有歸類問題的數據,然後將該表連接到主查詢的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 

感謝大家的幫助。

0

取出列並進入查詢狀態,其中不會引發錯誤,然後開始逐個添加列,並在其中引發與其相關的列的錯誤檢查,並在查詢中添加類似如下內容在WHERE Clause

WHERE Table1.Collation1Col COLLATE DATABASE_DEFAULT = Table2.Collation2Col COLLATE DATABASE_DEFAULT 

爲表進一步調查檢查列中的排序規則是從其他列不同。使用

SELECT name, collation_name 
FROM sys.columns 
WHERE OBJECT_ID IN (SELECT OBJECT_ID 
FROM sys.objects 
WHERE type = 'U' 
AND name = 'table_Name') 

它會縮小您的搜索的罪魁禍首。

+0

所以我縮小了這個問題。看起來在排序衝突的兩個表中,我使用INT作爲我連接的字段,另一個表使用nvarchar作爲鏈接字段。 當我試圖將它們轉換爲相同的(無論是int或Nvarchar)我得到的錯誤不能使用INT整理。 GRRRRR 所以我想出了使用Table變量並用兩個衝突表中的值填充它的想法。到現在爲止還挺好。 但是,我似乎無法得到CTE認可的表變量。 我該怎麼做? – CraigBob

+0

加入表格只需將FK列轉換爲PK列數據類型類似... ON Table1.ID = CAST(Table2.Column AS INT) –

+0

我試過了,它給了我錯誤不能整理(INT) 而且我不能在CTE中使用@table變量。我不得不創建一個真正的臨時表。所以現在我需要弄清楚如何將#temp表加入CTE。 #temp表有兩部分的關鍵,因爲storeID可以重複(它們來自不同的源系統,我很難過。 – CraigBob