我有一个像这样的表变量@WEEKS_MONTH
:
我必须使用query:
更新此表(列SUMMARY
)
SELECT SUM(AVAI_NUM)
FROM SCAVAI
WHERE AVAI_DST >= (SELECT WEEKSTART
FROM @WEEKS_MONTH
WHERE WEEKOFMONTH = 1)
AND AVAI_DST <= (SELECT WEEKEND
FROM @WEEKS_MONTH
WHERE WEEKOFMONTH = 1)
我遇到的问题是我不知道如何将该表与此查询连接起来。 (WEEKOFMONTH
必须逐行显示在表格中。例如,我只是输入数字1.)
答案 0 :(得分:3)
只需在UPDATE
中使用子查询:
UPDATE wm
SET Summary = (SELECT SUM(s.AVAI_NUM)
FROM SCAVAI s
WHERE s.AVAI_DST BETWEEN wm.WEEK_START and wm.WEEK_END)
FROM @WEEKS_MONTH wm