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 回答
0

此查询问题的根本原因实际上是表 ItemBalance 和 TPartAdjustmentC 的别名冲突。

用于并集 All 中的表的相同别名 (a) 会使比较零件编号的条件不明确。

于 2018-08-15T04:58:24.903 回答