我有一个包含user_id和visited_at列的访问表。我想知道本月每天有多少是新客户和重复客户。
user_id visited_at
--------------------------------------------------------------------------
1750 2015-01-28 12:38:59
1920 2015-01-19 17:20:20
1009 2015-01-17 18:20:12
1920 2015-01-17 15:10:10
1080 2015-01-13 20:18:41
1920 2014-04-04 10:31:15
1750 2013-10-04 10:39:20
2015年1月,用户1750和1920访问了同一个地方,因此总重复客户数为2. 2015年4月,用户1750,1920和1080访问了同一个地方,因此总重复客户数为3.输出应该是这样的
October 2013
Month New Repeat
----------------------------------------------
2013-10-04 1 0
April 2014
Month New Repeat
----------------------------------------------
2014-04-04 1 0
January 2015
Month New Repeat
----------------------------------------------
2015-01-13 1 0
2015-01-17 1 1
2015-01-19 0 1
2015-01-28 0 1
答案 0 :(得分:2)
乔普拉。 。 。实际上,我确实误解了你在评论中提出的问题。这与按月的结构非常相似,只是按日期汇总:
select date(v.visited_at),
count(case when v.visited_at = vv.minva then user_id end) as num_new_users,
(count(distinct user_id) - count(case when v.visited_at = vv.minva then user_id end)
) as num_repeat_users
from visits v join
(select user_id, min(visited_at) as minva
from visits t
group by user_id
) vv
on v.user_id = vv.user_id
group by date(v.visited_at)
order by date(v.visited_at);