Sql 雅典娜得到每组中的最小值和相应的其他列值
输入表Sql 雅典娜得到每组中的最小值和相应的其他列值,sql,datetime,greatest-n-per-group,presto,amazon-athena,Sql,Datetime,Greatest N Per Group,Presto,Amazon Athena,输入表 user id action date collection aaa 1 view 2020-09-01 {some JSON data_1} aaa 1 view 2020-09-02 {some JSON data_2} aaa 1 view 2020-09-03 {some JSON data_3} bbb 2 view 2020-09-08 {some JSON data_22} bb
user id action date collection
aaa 1 view 2020-09-01 {some JSON data_1}
aaa 1 view 2020-09-02 {some JSON data_2}
aaa 1 view 2020-09-03 {some JSON data_3}
bbb 2 view 2020-09-08 {some JSON data_22}
bbb 2 view 2020-09-09 {some JSON data_23}
ccc 2 view 2020-09-01 {some JSON data_99}
ddd 3 view 2020-09-01 {some JSON data_88}
user id action date collection
aaa 1 view 2020-09-01 {some JSON data_1}
bbb 2 view 2020-09-08 {some JSON data_22}
ccc 2 view 2020-09-01 {some JSON data_99}
ddd 3 view 2020-09-01 {some JSON data_88}
输出表
user id action date collection
aaa 1 view 2020-09-01 {some JSON data_1}
aaa 1 view 2020-09-02 {some JSON data_2}
aaa 1 view 2020-09-03 {some JSON data_3}
bbb 2 view 2020-09-08 {some JSON data_22}
bbb 2 view 2020-09-09 {some JSON data_23}
ccc 2 view 2020-09-01 {some JSON data_99}
ddd 3 view 2020-09-01 {some JSON data_88}
user id action date collection
aaa 1 view 2020-09-01 {some JSON data_1}
bbb 2 view 2020-09-08 {some JSON data_22}
ccc 2 view 2020-09-01 {some JSON data_99}
ddd 3 view 2020-09-01 {some JSON data_88}
如果我们看到输入表和输出表
我想要类似的
group by (user,id,action) then i need min(date) and corresponding collection value
有人能提出一个想法吗?一个选项是使用子查询进行查询:
select t.*
from mytable t
where t.date = (
select min(t1.date) from mytable t1 where t1.user = t.user
)
另一种解决方案是使用窗口函数按日期对具有相同用户的记录进行排序,然后使用该信息过滤结果集:
select *
from (
select t.*, row_number() over(partition by user order by date) rn
from mytable t
) t
where rn = 1