因此,我使用ROW_NUMBER()OVER(PARTITION BY)查询返回数据和行号,并将其放入临时表中。初始输出显示屏幕截图:
。
在这里,我需要在bt_newlabel列中分别替换为null。因此,行号1-4正在进行中,行号5-9将被承保,行号10-13将被执行,依此类推。
我碰壁试图确定该如何做。感谢您对我的处理方法的帮助或投入。
答案 0 :(得分:1)
一种方法是先分配组,然后分配值。如:
select t.*, max(bt_newlabel) over (partition by grp) as new_newlabel
from (select t.*, count(bt_newlabel) over (order by bt_stamp) as grp
from t
) t;
该组只是先前在数据中看到的已知值的数量。
您可以使用以下方式更新该字段:
with toupdate as (
select t.*, max(bt_newlabel) over (partition by grp) as new_newlabel
from (select t.*, count(bt_newlabel) over (order by bt_stamp) as grp
from t
) t
)
update toupdate
set bt_newlabel = new_newlabel
where bt_newlabel is null;
答案 1 :(得分:0)
如果我了解您要执行的操作,则这是您需要在临时表上执行的更新类型:
--This will update rows 1-4 to 'Pre-Underwritting'
UPDATE temp_table SET bt_newlabel = 'Pre-Underwritting'
WHERE rownumber between
1 AND (SELECT TOP 1 rownumber FROM temp_table WHERE bt_oldlabel = 'Pre-Underwritting');
--This will update rows 5-9 to 'Underwritting'
UPDATE temp_table SET bt_newlabel = 'Underwritting'
WHERE rownumber between
(SELECT TOP 1 rownumber FROM temp_table WHERE bt_oldlabel = 'Pre-Underwritting')
AND
(SELECT TOP 1 rownumber FROM temp_table WHERE bt_oldlabel = 'Underwritting');
--This will update rows 10-13 to 'Implementation'
UPDATE temp_table SET bt_newlabel = 'Implementation'
WHERE rownumber between
(SELECT TOP 1 rownumber FROM temp_table WHERE bt_oldlabel = 'Underwritting')
AND
(SELECT TOP 1 rownumber FROM temp_table WHERE bt_oldlabel = 'Implementation');
我制作了一个有效的小提琴以检查结果:http://sqlfiddle.com/#!18/1cae2/1/3