0

エイリアスが原因でこのエラーが発生し、少し直面していますが、私の頭は何かで振り返っています。エイリアスの間違ったチェックを解決するのを手伝ってもらえますか

SELECT B.PARTS_NO, B.Adj_Date, B.COST, B.ADJ_DESC, ADJ_FG_QTY_PREV = SUM(B.ADJ_FG_QTY_PREV), ADJ_COGS_QTY_PREV = SUM(B.ADJ_COGS_QTYPREV) , ADJ_FG_QTY = SUM(B.ADJ_FG_QTY), ADJ_COGS_QTY = SUM(B.ADJ_COGS_QTY)
 FROM 
 (                                                      
 SELECT                                                         
 PARTS_NO=a.PARTS_NO,                                                       
 Adj_Date=Convert(varchar(10),a.PROCESSING_DATETIME,105),                                                       
 COST=ISNULL((SELECT CASE WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=1 THEN ISNULL(a.ECOST_01,0)                                                      
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=2 THEN ISNULL(a.ECOST_02,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=3 THEN ISNULL(a.ECOST_03,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=4 THEN ISNULL(a.ECOST_04,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=5 THEN ISNULL(a.ECOST_05,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=6 THEN ISNULL(a.ECOST_06,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=7 THEN ISNULL(a.ECOST_07,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=8 THEN ISNULL(a.ECOST_08,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=9 THEN ISNULL(a.ECOST_09,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=10 THEN ISNULL(a.ECOST_10,0)                                                   
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=11 THEN ISNULL(a.ECOST_11,0)                                                   
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=12 THEN ISNULL(a.ECOST_12,0)                                                   
    ELSE 0                                                  
   END                                                                      
FROM TITEMBALANCE a                                                         
WHERE a.[YEAR]=YEAR(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))                                                       
AND a.PARTS_NO=a.PARTS_NO),0),      
 ADJ_DESC ='CATS',                                              
 ADJ_FG_QTY_PREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))<>ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                        
 ADJ_COGS_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))<>ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                       
 ADJ_FG_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))=ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                      
 ADJ_COGS_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))=ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end)                                                                                             
 FROM TPARTADJUSTMENTC a  INNER JOIN TPARTADJUSTMENTCL b ON a.CARD_NO=b.CARD_NO AND a.INHOUSE_OUTSIDE=b.INHOUSE_OUTSIDE AND a.PARTS_NO=b.PARTS_NO AND a.PROCESS_CODE=b.PROCESS_CODE AND a.FILLER=b.FILLER AND a.PROCESSING_DATETIME=b.PROCESSING_DATETIME                                                       
 WHERE ISNULL(a.COSTING1,0)=Convert(numeric(6,0),@Period)                                                       
 GROUP BY a.PARTS_NO,Convert(varchar(10),a.PROCESSING_DATETIME,105),Left(Ltrim(b.REASON_CODE),1)                                                        
 UNION ALL                                                      
 SELECT                                                         
 PARTS_NO=a.PARTS_NO,                                                       
 Adj_Date=Convert(varchar(10),a.PROCESSING_DATETIME,105),                                                       
 COST=ISNULL((SELECT CASE WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=1 THEN ISNULL(a.ECOST_01,0)                                                      
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=2 THEN ISNULL(a.ECOST_02,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=3 THEN ISNULL(a.ECOST_03,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=4 THEN ISNULL(a.ECOST_04,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=5 THEN ISNULL(a.ECOST_05,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=6 THEN ISNULL(a.ECOST_06,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=7 THEN ISNULL(a.ECOST_07,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=8 THEN ISNULL(a.ECOST_08,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=9 THEN ISNULL(a.ECOST_09,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=10 THEN ISNULL(a.ECOST_10,0)                                                   
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=11 THEN ISNULL(a.ECOST_11,0)                                                   
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=12 THEN ISNULL(a.ECOST_12,0)                                                   
    ELSE 0                                                  
   END                                                                      
FROM TITEMBALANCE a                                                         
WHERE a.[YEAR]=YEAR(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))                                                       
AND a.PARTS_NO=a.PARTS_NO),0),
 ADJ_DESC ='IMPULSE',                                                                                                                   
 ADJ_FG_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))<>ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                     
 ADJ_COGS_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))<>ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                       
 ADJ_FG_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))=ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                      
 ADJ_COGS_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),a.PROCESSING_DATETIME,112))=ISNULL(a.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end)                                                             
 FROM TPARTADJUSTMENTI a  INNER JOIN TPARTADJUSTMENTIL b ON a.CARD_NO=b.CARD_NO AND a.INHOUSE_OUTSIDE=b.INHOUSE_OUTSIDE AND a.PARTS_NO=b.PARTS_NO AND a.PROCESS_CODE=b.PROCESS_CODE AND a.FILLER=b.FILLER AND a.PROCESSING_DATETIME=b.PROCESSING_DATETIME                                                       
 WHERE ISNULL(a.COSTING1,0)=Convert(numeric(6,0),@Period)                                                       
 GROUP BY a.PARTS_NO,Convert(varchar(10),a.PROCESSING_DATETIME,105),Left(Ltrim(b.REASON_CODE),1)                                                        

 ) B GROUP BY B.PARTS_NO, B.Adj_Date, B.COST, B.ADJ_DESC
4

3 に答える 3

2

サブクエリとクエリのa両方でエイリアスを使用しています。TITEMBALANCETPARTADJUSTMENTC

a.PARTS_NO=a.PARTS_NO条件は両方のテーブルのフィールドを比較することになっていると思いますが、エイリアスが競合しているため、フィールドをそれ自体と比較するだけで、クエリはすべての部分を返します。これにより、サブクエリが複数のレコードを返すようになり、そのエラー メッセージが表示されます。

于 2013-05-06T07:47:05.800 に答える
0

これは答えに役立つかもしれません.2つのユニオン間で2つの名前エイリアスを変更しました. みんな、ありがとう

SELECT @ProsesPeriode as TanggalPeriod , B.PARTS_NO, B.Adj_Date, B.COST, B.ADJ_DESC, ADJ_FG_QTY_PREV = SUM(B.ADJ_FG_QTY_PREV), ADJ_COGS_QTY_PREV = SUM(B.ADJ_COGS_QTYPREV) , ADJ_FG_QTY = SUM(B.ADJ_FG_QTY), ADJ_COGS_QTY = SUM(B.ADJ_COGS_QTY)
 FROM 
 (                                                      
 SELECT                                                         
 PARTS_NO=d.PARTS_NO,                                                       
 Adj_Date=Convert(varchar(10),d.PROCESSING_DATETIME,105),                                                       
 COST=ISNULL((SELECT CASE WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=1 THEN ISNULL(a.ECOST_01,0)                                                      
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=2 THEN ISNULL(a.ECOST_02,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=3 THEN ISNULL(a.ECOST_03,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=4 THEN ISNULL(a.ECOST_04,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=5 THEN ISNULL(a.ECOST_05,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=6 THEN ISNULL(a.ECOST_06,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=7 THEN ISNULL(a.ECOST_07,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=8 THEN ISNULL(a.ECOST_08,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=9 THEN ISNULL(a.ECOST_09,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=10 THEN ISNULL(a.ECOST_10,0)                                                   
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=11 THEN ISNULL(a.ECOST_11,0)                                                   
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=12 THEN ISNULL(a.ECOST_12,0)                                                   
    ELSE 0                                                  
   END                                                                      
FROM TITEMBALANCE a                                                         
WHERE a.[YEAR]=YEAR(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))                                                       
AND a.PARTS_NO=d.PARTS_NO),0),      
 ADJ_DESC ='CATS',                                              
 ADJ_FG_QTY_PREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),d.PROCESSING_DATETIME,112))<>ISNULL(d.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                        
 ADJ_COGS_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),d.PROCESSING_DATETIME,112))<>ISNULL(d.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                       
 ADJ_FG_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),d.PROCESSING_DATETIME,112))=ISNULL(d.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                      
 ADJ_COGS_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),d.PROCESSING_DATETIME,112))=ISNULL(d.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end)                                                                                             
 FROM TPARTADJUSTMENTC d  INNER JOIN TPARTADJUSTMENTCL b ON d.CARD_NO=b.CARD_NO AND d.INHOUSE_OUTSIDE=b.INHOUSE_OUTSIDE AND d.PARTS_NO=b.PARTS_NO AND d.PROCESS_CODE=b.PROCESS_CODE AND d.FILLER=b.FILLER AND d.PROCESSING_DATETIME=b.PROCESSING_DATETIME                                                       
 WHERE ISNULL(d.COSTING1,0)=Convert(numeric(6,0),@Period)                                                       
 GROUP BY d.PARTS_NO,Convert(varchar(10),d.PROCESSING_DATETIME,105),Left(Ltrim(b.REASON_CODE),1)                                                        
 UNION ALL                                                      
 SELECT                                                         
 PARTS_NO=k.PARTS_NO,                                                       
 Adj_Date=Convert(varchar(10),k.PROCESSING_DATETIME,105),                                                       
 COST=ISNULL((SELECT CASE WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=1 THEN ISNULL(a.ECOST_01,0)                                                      
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=2 THEN ISNULL(a.ECOST_02,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=3 THEN ISNULL(a.ECOST_03,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=4 THEN ISNULL(a.ECOST_04,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=5 THEN ISNULL(a.ECOST_05,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=6 THEN ISNULL(a.ECOST_06,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=7 THEN ISNULL(a.ECOST_07,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=8 THEN ISNULL(a.ECOST_08,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=9 THEN ISNULL(a.ECOST_09,0)                                                    
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=10 THEN ISNULL(a.ECOST_10,0)                                                   
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=11 THEN ISNULL(a.ECOST_11,0)                                                   
    WHEN MONTH(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))=12 THEN ISNULL(a.ECOST_12,0)                                                   
    ELSE 0                                                  
   END                                                                      
FROM TITEMBALANCE a                                                         
WHERE a.[YEAR]=YEAR(Convert(Datetime,LTRIM(RTRIM(@Period))+'01',112))                                                       
AND a.PARTS_NO=k.PARTS_NO),0),
 ADJ_DESC ='IMPULSE',                                                                                                                   
 ADJ_FG_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),k.PROCESSING_DATETIME,112))<>ISNULL(k.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                     
 ADJ_COGS_QTYPREV=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),k.PROCESSING_DATETIME,112))<>ISNULL(k.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                       
 ADJ_FG_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),k.PROCESSING_DATETIME,112))=ISNULL(k.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='1' THEN b.QUANTITY_F Else 0 End) else 0 end),                                                      
 ADJ_COGS_QTY=SUM(CASE WHEN Convert(numeric(6,0),Convert(varchar(6),k.PROCESSING_DATETIME,112))=ISNULL(k.COSTING1,0) THEN (CASE WHEN Left(Ltrim(b.REASON_CODE),1)='2' THEN b.QUANTITY_F Else 0 End) else 0 end)                                                             
 FROM TPARTADJUSTMENTI k  INNER JOIN TPARTADJUSTMENTIL b ON k.CARD_NO=b.CARD_NO AND k.INHOUSE_OUTSIDE=b.INHOUSE_OUTSIDE AND k.PARTS_NO=b.PARTS_NO AND k.PROCESS_CODE=b.PROCESS_CODE AND k.FILLER=b.FILLER AND k.PROCESSING_DATETIME=b.PROCESSING_DATETIME                                                       
 WHERE ISNULL(k.COSTING1,0)=Convert(numeric(6,0),@Period)                                                       
 GROUP BY k.PARTS_NO,Convert(varchar(10),k.PROCESSING_DATETIME,105),Left(Ltrim(b.REASON_CODE),1)                                                        

 ) B GROUP BY B.PARTS_NO, B.Adj_Date, B.COST, B.ADJ_DESC
于 2013-05-06T08:29:11.397 に答える