我正在尝试在GROUP中的后续行中重复行值。一个组可以有一个或多个TAG。要求是在填充TAG的行和后续行中填充NEW_TAG,直到在同一组中填充另一个TAG,或者我们到达该GROUP的末尾。
Current Table Required Table GROUPID SEQ TAG GROUPID SEQ TAG NEW_TAG ------- --- ---- ------- --- --- -------- 1 1 1 1 1 2 1 2 1 3 1 3 1 4 4 1 4 4 4 1 5 1 5 4 1 6 1 6 4 1 7 1 7 4 1 8 1 8 4 2 1 2 1 2 2 2 2 2 3 2 3 2 4 2 4 2 5 5 2 5 5 5 2 6 2 6 5 2 7 2 7 5 2 8 2 8 5 2 9 9 2 9 9 9 2 10 2 10 9 2 11 2 11 9
由于
答案 0 :(得分:0)
假设TAG总是在增加
max(TAG) over
(
partition by GROUPID
order by SEQ
rows between unbounded preceding
and current row
) as NEW_TAG
select *
,max(TAG) over
(
partition by GROUPID
order by SEQ
rows between unbounded preceding
and current row
) as NEW_TAG
from mytable
;
+---------+--------+--------+---------+
| groupid | seq | tag | new_tag |
+---------+--------+--------+---------+
| 1 | 1 | | |
| 1 | 2 | | |
| 1 | 3 | | |
| 1 | 4 | 4 | 4 |
| 1 | 5 | | 4 |
| 1 | 6 | | 4 |
| 1 | 7 | | 4 |
| 1 | 8 | | 4 |
| 2 | 1 | | |
| 2 | 2 | | |
| 2 | 3 | | |
| 2 | 4 | | |
| 2 | 5 | 5 | 5 |
| 2 | 6 | | 5 |
| 2 | 7 | | 5 |
| 2 | 8 | | 5 |
| 2 | 9 | 9 | 9 |
| 2 | 10 | | 9 |
| 2 | 11 | | 9 |
+---------+--------+--------+---------+