按周数分组

时间:2016-10-31 19:22:23

标签: mysql sql

我希望按周数进行去年的队列分析

输出结果为:

weeknumber session_count acq_count

37           5           3

43           2           2

...

到目前为止的MySql:

SELECT WEEK(datetime) AS weeknumber, COUNT(*) AS session_count, 
   acquisition as acq_count=(SELECT COUNT(*) FROM sessions WHERE acquisition=1 ) 
FROM sessions WHERE `datetime` >= NOW()- INTERVAL 365 DAY 
GROUP BY weeknumber

挑战:如何在同一个sql查询中获取匹配acquire = 1的行数?

感谢您的想法! 佛瑞德

1 个答案:

答案 0 :(得分:1)

我想你想要这个查询

SELECT WEEK(datetime) AS weeknumber, COUNT(*) AS session_count, 
   SUM(acquisition=1) as acq_count 
FROM sessions WHERE `datetime` >= NOW()- INTERVAL 365 DAY 
GROUP BY weeknumber