输入:
ID CREATED_TIME CANCELED_TIME
1 4 10
1 8 2
1 6 -1
1 3 7
2 5 null
2 4 8
期望的输出:
ID CREATED_TIME CANCELED_TIME
1 3 2
2 4 null
所以我基本上想显示每个用户的 created_time 最大的行的 id、min(created_time) 和取消时间?请在 PostgreSql 和 MySql 中提供答案?
最佳答案
如果您想要 canceled_time
其中 created_time
具有最大值,我建议使用 substring_index()
/group_concat()
技巧:
select id, min(created_time),
substring_index(group_concat(canceled_time order by created_time desc), ',', 1) as canceled_time
from table
group by id;
这在使用默认设置的所有情况下都不起作用,因为 group_concat()
中间结果有最大长度。但是,它确实适用于最合理的数据集,而且绝对适用于您问题中的数据。
关于mysql - mysql中如何显示一列的最小值并使用同一列的最大值来过滤其他属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28830877/