简体   繁体   English

Athena 获取每组中的最小值和对应的其他列值

[英]Athena get the minimum value in each group and corresponding other column values

Input Table输入表

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}

Output_Table输出表

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}

if we see input table and output_table,如果我们看到输入表和输出表,

i want similar to this我想要类似的

group by (user,id,action) then i need min(date) and corresponding collection value

Can anyone suggest an idea?任何人都可以提出一个想法吗?

One option is to flter with a subquery:一种选择是使用子查询进行过滤:

select t.*
from mytable t
where t.date = (
    select min(t1.date) from mytable t1 where t1.user = t.user
)

Another solution is to use window functions to rank records having the same user by date , then use that information to filter the resultset:另一种解决方案是使用窗口函数按date具有相同user的记录进行排名,然后使用该信息过滤结果集:

select *
from (
    select t.*, row_number() over(partition by user order by date) rn
    from mytable t
) t
where rn = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Postgres:获取与组中其他列的最大值对应的列的值 - Postgres: Get value of a column corresponding to max of other column in a group 在 SQL/Athena 中通过一组列获取最大值 - Get max value by a group of column in SQL/Athena SQL Server-每个对应值的按列分组 - SQL Server - group by column for each corresponding value 按名称分组一列,并以pandas为单位获取相应的值 - group a column by names and get the corresponding values in pandas 对应于另一列值的列的最小值 - Minimum value of a column corresponding to another column value 如何选择彼此对应的列值? - How do I select column values to be corresponding to each other? 在SQL中,如何选择列的最小值和其他列的组? - In SQL, how to select minimum value of a column and group by other columns? Select 行使用 group by 并且在每个组中获取基于另一个列值的最高值的列值 - Select rows using group by and in each group get column values based on highest of another column value 如何获得每个组中的最大值和最小值以及它们的时间? - How to get the maximum and minimum values in each group as well as their times? 获得下一个最小值,大于或等于每个组的给定值 - Get next minimum, greater than or equal to a given value for each group
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM