我有CTE
,如下所示:
;with cte as
(
SELECT WeekNum
FROM MonitorTable
GROUP BY WeekNum
HAVING SUM(CASE WHEN IsProcessed = 1 THEN 1
ELSE 0
END) = 8
order by WeekNum
)
update MonitorTable
set ReadToGrid = 0
where WeekNum in (select WeekNum from cte)
这里的问题是它正在更新所有内容。由于总是必须剩下两个,我只能更新除了最后一个以外的所有内容。
所以,让我们说CTE会返回以下内容:
1
2
3
4
5
6
我无法更新所有内容;需要更新除最后两个(1,2,3,4)之外的所有内容。
那么如何修改脚本以便更新除最后两行之外的所有内容?
感谢。
答案 0 :(得分:1)
没有数据我无法测试确切的语法,但这个想法会起作用:
Select WeekNum, Row_Number() Over (Order By WeekNum Desc) RowNum
From
(
SELECT WeekNum
FROM MonitorTable
GROUP BY WeekNum
HAVING SUM(CASE WHEN IsProcessed = 1 THEN 1
ELSE 0
END) = 8
)
update MonitorTable set ReadToGrid = 0 where WeekNum in (select WeekNum from cte Where RowNum > 2)