聚合可能不会出现在UPDATE语句的集合列表中

时间:2015-07-13 14:49:46

标签: sql sql-server sql-server-2008

收到说明聚合的错误可能不会出现在UPDATE语句的集合列表中。有什么想法吗?

 UPDATE STOCK SET STOCK.LPURCHDATE = MAX(INVTRANS.TRANS_DATE)
 FROM STOCK
 INNER JOIN INVTRANS ON STOCK.NUMBER = INVTRANS.NUMBER
 WHERE INVTRANS.NOTATION <> 'Product Merging' AND
(INVTRANS.TRANSTYPE = 'D' OR INVTRANS.TRANSTYPE = 'A')

1 个答案:

答案 0 :(得分:0)

试试这个

UPDATE STOCK SET STOCK.LPURCHDATE = (select MAX(INVTRANS.TRANS_DATE) FROM INVTRANS
                                WHERE INVTRANS.NOTATION <> 'Product Merging' 
                                AND  INVTRANS.TRANSTYPE IN('D','A') 
                                AND STOCK.NUMBER = INVTRANS.NUMBER)

;with tmp as(
SELECT STOCK.NUMBER, MAX(INVTRANS.TRANS_DATE) MX
 FROM STOCK
 INNER JOIN INVTRANS ON STOCK.NUMBER = INVTRANS.NUMBER
 WHERE INVTRANS.NOTATION <> 'Product Merging' AND
(INVTRANS.TRANSTYPE = 'D' OR INVTRANS.TRANSTYPE = 'A')
)
 UPDATE STOCK SET STOCK.LPURCHDATE = MX
 FROM STOCK
 INNER JOIN TMP ON STOCK.NUMBER = TMP.NUMBER