我有一个包含时间(UTC)和accountID字段的表。
accountID | time | ...
1 |12:00 |....
1 |12:01 |...
1 |13:00 |...
2 |14:00 |...我需要进行一个sql查询,以返回一个新字段,该字段计数“accountID”,其中“类别”可以是“a”或“b”。如果来自同一accountID的行条目的正时差为1分钟或更短,则类别'a‘需要增加,否则'b’。上表的结果如下
accountID| cat a count| cat b count
1 | 1 | 2
2 | 0 | 1我可以采取什么方法来比较不同行之间的值和比较结果的输出情况?
谢谢
发布于 2020-02-10 15:21:53
使用lag()和条件聚合:
select accountid,
sum(prev_time >= time - interval 1 minute) as a_count,
sum(prev_time < time - interval 1 minute or prev_time is null) as b_count
from (select t.*,
lag(time) over (partition by accountid order by time) as prev_time
from t
) t
group by accountid;https://stackoverflow.com/questions/60153382
复制相似问题