我有时间的访问,时间戳记这样如何查找连续日期的最长序列?
ID, time
1, 1493596800
1, 1493596900
1, 1493432800
2, 1493596800
2, 1493596850
2, 1493432800
我用火花SQL数据库,我需要有像
ID, longest_seq (days)
1, 2
2, 5
3, 1
每个ID consecutives的最长序列中的日期我试着去适应这个回答Detect consecutive dates ranges using SQL对我来说,但我没有达到我的期望。
SELECT ID, MIN (d), MAX(d)
FROM (
SELECT ID, cast(from_utc_timestamp(cast(time as timestamp), 'CEST') as date) AS d,
ROW_NUMBER() OVER(
PARTITION BY ID ORDER BY cast(from_utc_timestamp(cast(time as timestamp), 'CEST')
as date)) rn
FROM purchase
where ID is not null
GROUP BY ID, cast(from_utc_timestamp(cast(time as timestamp), 'CEST') as date)
)
GROUP BY ID, rn
ORDER BY ID
如果有人对如何解决这一要求,或有什么错在它的一些线索,我将不胜感激帮助 感谢
[编辑]一个更明确的输入/输出
ID, time
1, 1
1, 2
1, 3
2, 1
2, 3
2, 4
2, 5
2, 10
2, 11
3, 1
3, 4
3, 9
3, 11
其结果将是:
ID, MaxSeq (in days)
1,3
2,3
3,1
所有的访问是在时间戳,但我需要连续几天,然后每天每次访问一天一次地计算在内
你能给出更具代表性的意见吗?我认为输入数据集不匹配结果。 –