我有两个带有时间戳列的表。
表#1包含点击次数,时间戳,表#2包含用户ID,时间戳。我希望按日期计算点击次数和用户数。例如
Date clicks_count users_count
2015-07-24 10 15
2015-07-24 04 06
答案 0 :(得分:0)
select date(timestamp),
sum(is_click) as clicks,
sum(is_click = 0) as user_count
from
(
select timestamp, 1 as is_click from table1
union all
select timestamp, 0 from table2
) tmp
group by date(timestamp)
您可以同时从两个表中选择timestamp
,并添加一个计算列,指示时间戳来自哪个表。
然后,您按照日期获取子查询结果和分组,并计算用户和点击次数。
sum(is_click = 0)
计算来自users表的时间戳的时间。
答案 1 :(得分:0)
我认为这些SQL对你有用。
select a.date1,clicks_count,users_count from
(select date(Table1.timestamp)as date1, count(clicks) as clicks_count
from Table1
group by date(Table1.timestamp)) as a
join
(
select date(Table2.timestamp) date2, count(userid) as users_count
from Table2
group by date(Table2.timestamp)) b on a.date1 = b.date2
谢谢。