有以下工作选择声明:
SELECT
ITEM,
LOW,
SUM(CASE WHEN ITEM_STATE = 'RT' THEN QUANTO*-1 ELSE ITEMS.QUANTO END) AS QUANTO_TOTAL
FROM ITEMS
INNER JOIN CMS
ON ITEMS.ORDERNO = CMS.ORDERNO
INNER JOIN STOCK
ON ITEMS.ITEM = STOCK.NUMBER
WHERE ODR_DATE BETWEEN '2014-03-28 00:00:00.000'
AND '2015-03-28 00:00:00.000'
GROUP BY ITEM, LOW
;
如何使用STOCK.LOW
更新QUANTO_TOTAL
?
答案 0 :(得分:0)
使用CTE
,然后将其加入STOCK
:
with cte as (
select ITEM,LOW,
sum( CASE
WHEN ITEM_STATE = 'RT' THEN QUANTO*-1
ELSE ITEMS.QUANTO
END) AS QUANTO_TOTAL
FROM ITEMS
INNER JOIN CMS ON ITEMS.ORDERNO = CMS.ORDERNO
INNER JOIN STOCK ON ITEMS.ITEM = STOCK.NUMBER
where ODR_DATE BETWEEN '2014-03-28 00:00:00.000' AND '2015-03-28 00:00:00.000'
GROUP BY ITEM,LOW)
update stock
set stock.low = cte.QUANTO_TOTAL
from cte
join stock on cte.ITEM = STOCK.NUMBER