2017-04-07 36 views
0

我試圖在GROUP中隨後的行中重複一行值。一個集團可以有一個或多個TAG。需求是在填充TAG的行和後續行中填充NEW_TAG,直到在同一組中填充另一個TAG,或者我們到達該組的末尾。在HIVE中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

假設標籤是始終不斷增加

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  | 
+---------+--------+--------+---------+ 
+0

謝謝Dudu Markovitz。這工作。 ! – Dhana

+0

太好了。請確保接受答案 –