SQL UPDATE有错误

时间:2016-09-14 14:38:05

标签: sql sql-update

感谢我对代码的任何帮助,我不断收到错误

  来自Toad的

SQL命令未正确结束。

UPDATE GS_MRC_EXISTING  GME
SET GME.GROSS_LAST = STC.SALES 
WITH STC AS 
( 
SELECT DISTINCT ( DSF.SOLD_TO_CUSTOMER_KEY ) AS CUSTS, 
           SUM ( CASE WHEN DSF.NET_PRICE >=0 THEN NET_PRICE ELSE 0 END  ) AS SALES,
           SUM ( CASE WHEN DSF.NET_PRICE <=0 THEN NET_PRICE ELSE 0 END  ) AS DISCOUNT
FROM    DAILY_SALES_FACT  DSF,
            MATERIAL_DIM MD    
WHERE 
DSF.SALES_DOC_TYPE_SID = '10'  
AND DSF.MATERIAL_SID = MD.MATERIAL_SID  
AND DSF.NET_PRICE <> 0
AND DSF.TPNM_ACTUAL_START_DATE_KEY BETWEEN '20160101' AND '20160731'   
GROUP BY DSF.SOLD_TO_CUSTOMER_KEY
)
WHERE GME.CUST_CODE = STC.CUSTS;

2 个答案:

答案 0 :(得分:1)

Toad建议Oracle。这表明这种update(或merge)的子查询:

UPDATE GS_MRC_EXISTING GME
    SET GME.GROSS_LAST = (SELECT SUM(CASE WHEN DSF.NET_PRICE >= 0 THEN NET_PRICE ELSE 0 END  ) AS SALES
                          FROM DAILY_SALES_FACT DSF JOIN
                               MATERIAL_DIM MD  
                               ON DSF.MATERIAL_SID = MD.MATERIAL_SID  
                          WHERE DSF.SALES_DOC_TYPE_SID = '10' AND
                                DSF.NET_PRICE <> 0 AND
                                DSF.TPNM_ACTUAL_START_DATE_KEY BETWEEN '20160101' AND '20160731' AND
                                DSF.SOLD_TO_CUSTOMER_KEY = GME.CUST_CODE
                         );

注意:这会更新所有行,甚至是那些不匹配的行。您可能希望限制此:

UPDATE GS_MRC_EXISTING GME
    SET GME.GROSS_LAST = (SELECT SUM(CASE WHEN DSF.NET_PRICE >= 0 THEN NET_PRICE ELSE 0 END  ) AS SALES
                          FROM DAILY_SALES_FACT DSF JOIN
                               MATERIAL_DIM MD  
                               ON DSF.MATERIAL_SID = MD.MATERIAL_SID  
                          WHERE DSF.SALES_DOC_TYPE_SID = '10' AND
                                DSF.NET_PRICE <> 0 AND
                                DSF.TPNM_ACTUAL_START_DATE_KEY BETWEEN '20160101' AND '20160731' AND
                                DSF.SOLD_TO_CUSTOMER_KEY = GME.CUST_CODE
                         )
WHERE EXISTS (SELECT 1
              FROM DAILY_SALES_FACT DSF JOIN
                   MATERIAL_DIM MD  
                   ON DSF.MATERIAL_SID = MD.MATERIAL_SID  
              WHERE DSF.SALES_DOC_TYPE_SID = '10' AND
                    DSF.NET_PRICE <> 0 AND
                    DSF.TPNM_ACTUAL_START_DATE_KEY BETWEEN '20160101' AND '20160731' AND
                    DSF.SOLD_TO_CUSTOMER_KEY = GME.CUST_CODE
             );

答案 1 :(得分:0)

也许你应该删除&#34; UPDATE&#34;命令&#34; WITH&#34;部分。如果您按字段对结果进行分组,则可能不会使用&#34; distinct&#34;在那个领域。像这样:

WITH STC AS
(
    SELECT DSF.SOLD_TO_CUSTOMER_KEY AS CUSTS,
      SUM ( CASE WHEN DSF.NET_PRICE >=0 THEN NET_PRICE ELSE 0 END  ) AS SALES,
      SUM ( CASE WHEN DSF.NET_PRICE <=0 THEN NET_PRICE ELSE 0 END  ) AS DISCOUNT
    FROM    DAILY_SALES_FACT  DSF,
      MATERIAL_DIM MD
    WHERE
      DSF.SALES_DOC_TYPE_SID = '10'
      AND DSF.MATERIAL_SID = MD.MATERIAL_SID
      AND DSF.NET_PRICE <> 0
      AND DSF.TPNM_ACTUAL_START_DATE_KEY BETWEEN '20160101' AND '20160731'
    GROUP BY DSF.SOLD_TO_CUSTOMER_KEY
)
UPDATE GS_MRC_EXISTING SET GROSS_LAST = STC.SALES WHERE CUST_CODE = STC.CUSTS;