SQL子查询插入错误

时间:2016-06-14 12:17:14

标签: sql insert

我试图在表格中插入一个值,但是根据Webtype的值,但我收到了错误。

INSERT INTO GS_WEBSITES_LIVE (THIS_MONTH) 
( SELECT 
    COUNT ( DISTINCT LPL.SOLD_TO_CUSTOMER_KEY ) AS THIS_MONTH
FROM 
    BW_SHARE.LP_LIVE_MAY16 LPL    
WHERE 
    LPL.MATERIAL = 'ECOM'
)
WHERE WEBTYPE = 'WEB';

1 个答案:

答案 0 :(得分:0)

您的子查询没有名为WEBTYPE的列,这是导致错误的直接原因。所以,你可能想要这个:

INSERT INTO GS_WEBSITES_LIVE (THIS_MONTH) 
    SELECT COUNT( DISTINCT LPL.SOLD_TO_CUSTOMER_KEY ) AS THIS_MONTH
    FROM BW_SHARE.LP_LIVE_MAY16 LPL    
    WHERE LPL.MATERIAL = 'ECOM' AND WEBTYPE = 'WEB';

但是,我怀疑你想要UPDATE

UPDATE GS_WEBSITES_LIVE
    SET THIS_MONTH = (SELECT COUNT(DISTINCT LPL.SOLD_TO_CUSTOMER_KEY ) AS THIS_MONTH
                      FROM BW_SHARE.LP_LIVE_MAY16 LPL    
                      WHERE LPL.MATERIAL = 'ECOM'
                     )
    WHERE WEBTYPE = 'WEB';