在保留日期列的同时插入表关闭行

时间:2017-02-14 14:34:47

标签: sql join hive

遵循question

我的表

id    sum    type    date 
1     3      -1      2017-02-02
1     6      -1      2017-02-04
1     -6      2      2017-02-01
1     -3      1      2017-02-09
1     3      -1      2017-02-17
1     6      -1      2017-02-05

此查询查找传递条件的人员,并返回这些用户的occurrences行数,并修改了一些列。

with t as(
select id
  , -abs (sum) as sum   
  , sum (case when type = -1 then 1 else -1 end) as occurrences
  --, collect_list(date) as time_col
from table
group by id, abs(sum)
having sum (case when type = -1 then 1 else -1 end) > 15
)
select t.id
  , t.sum  
  , 2 as type
from t 
lateral view explode (split (space (cast (occurrences as int) - 1),' ')) e
-- lateral view explode(time_col) time_table as time_key;

问题是,我需要每一行都能从列表中保存一个日期列。我尝试添加collect_list(date) as time_col然后

lateral view explode(time_col) time_table as time_key;

但这只是返回了所有可能的组合。我可能会使用一个连接(会起作用吗?),但我想知道这是否真的有必要。

最后这些行

1     3      -1      2017-02-17
1     6      -1      2017-02-05

会变成

1     -3      2    2017-02-17
1     -6      2    2017-02-05

1 个答案:

答案 0 :(得分:1)

select      val_id
           ,-val_sum    as val_sum
           ,2           as val_type
           ,val_date

from       (select      val_id
                       ,val_sum
                       ,val_type
                       ,val_date

                       ,sum (case when val_type = -1 then 1 else -1 end) over   
                        (
                            partition by    val_id,-abs (val_sum) 
                        ) as occurrences

                       ,row_number () over
                        (
                            partition by    val_id,val_sum 
                            order by        val_date desc
                        )   as rn

            from        mytable
            ) t

where       val_type     =  -1
        and rn          <=  occurrences
        and occurrences >   15
;

执行结果(不含and occurrences > 15

+--------+---------+----------+------------+
| val_id | val_sum | val_type | val_date   |
+--------+---------+----------+------------+
| 1      | -3      | 2        | 2017-02-17 |
+--------+---------+----------+------------+
| 1      | -6      | 2        | 2017-02-05 |
+--------+---------+----------+------------+