如果我有一张包含以下内容的表格:
user_id | event_time
如何计算事件之间的平均天数?得到类似的东西:
days_diff | count
1 | 100
2 | 90
3 | 20
用户可能在事件之间有1天,但在后续事件之间也可能有3天。我如何在两个桶中统计它们?
示例数据(请注意,在这种情况下,DAY DIFF为0/1,但这只是一小部分数据)
user_id | event_time
82770 2015-05-04 02:34:53
1 2015-05-04 08:45:53
82770 2015-05-04 20:38:24
82770 2015-05-04 20:38:24
82770 2015-05-04 20:38:24
1 2015-05-05 09:31:42
82770 2015-05-05 13:33:36
82770 2015-05-05 13:33:53
1 2015-05-06 09:53:59
1 2015-05-06 23:31:18
1 2015-05-06 23:31:35
1 2015-05-07 12:31:41
82770 2015-05-07 16:01:16
答案 0 :(得分:2)
这是一个不使用临时表的解决方案:
select daybetweenevents as days_diff,
count(daybetweenevents) as count
from (select t1.user_id,
t1.event_time,
datediff(day, t1.event_time, min(t2.event_time)) as daybetweenevents
from yourtable t1
inner join yourtable t2
on t1.user_id = t2.user_id
and t1.event_time < t2.event_time
group by t1.user_id, t1.event_time) temp
group by daybetweenevents
答案 1 :(得分:1)
使用DATEDIFF
和相关的子查询来获取上一个日期。
SELECT user_id, event_time,
DATEDIFF((SELECT MAX(event_time)
FROM yourtable
WHERE event_time < a.event_time), event_time) AS days_diff
FROM yourtable AS a
答案 2 :(得分:0)
我选择了一个有序用户事件的临时表,以便更轻松地进行相关性查找,并处理具有两个以上事件的用户。这应该可以为您提供所需的输出。
create table #tempOrderedUserEvents
(
id int identity (1,1),
userid int,
event_time datetime
)
insert into #tempOrderedUserEvents (userid, event_time)
select [user_id], event_time
from YourUserDataTable A
order by [user_id], event_time
select interval, count(*) as [count]
from
(
select A.userid, datediff(day, A.event_time, B.event_time) as interval
from #tempOrderedUserEvents A
JOIN #tempOrderedUserEvents B on A.id+1 = B.id and A.userid = B.userid
) as Intervals
group by interval
drop table #tempOrderedUserEvents