[英]MySQL split up groupBy clause over multiple columns
My knowledge of complex MySQL queries is not good and I'm having trouble searching or even phrasing this question. 我对复杂的MySQL查询的知识不好,并且在搜索甚至措辞上都遇到问题。
I have a table that looks as follows. 我有一个表,如下所示。
I have a query that counts all the rows where 'type = student' and then groups those by year and month for which I use the created_column. 我有一个查询,该查询计算“类型=学生”的所有行,然后按年份和月份将使用create_column的行进行分组。 This is pretty straightforward.
这很简单。
Query looks like this: 查询看起来像这样:
SELECT YEAR(created_at) as year, MONTH(created_at) as month, COUNT(type) as student_count
FROM users
WHERE type = "student"
GROUP BY year, month
ORDER BY year, month
I now need to add 2 more columns, 'subscription' & 'pay_per_video' which will further divide the total count for each month and year into student level type either 'subscription' OR 'pay_per_video'. 现在,我需要再添加2列“订阅”和“ pay_per_video”,这将把每个月和每年的总数进一步划分为学生级别类型的“订阅”或“ pay_per_video”。 Eg.
例如。 if in Jan 2017 a total of 20 students registered.
如果在2017年1月共有20名学生注册。 12 might be per_per_video students while 8 in that month were subscription students.
每视频学生12名,而当月有8名是订阅学生。 Any pointers are greatly appreciated.
任何指针,不胜感激。
UPDATE What I'm looking for is something like this: Table: Subject_Selection 更新我正在寻找的是这样的:表:Subject_Selection
Year Month Student Count Pay Per View Subscription
--------------------------------------------------------------------
2016 12 20 20 0
2017 1 23 12 11
2017 2 30 10 20
2017 3 2 1 1
2017 4 12 2 10
2017 5 90 40 50
2017 6 12 0 12
Use conditional aggregation like below: 使用如下条件聚合:
SELECT YEAR(created_at) as year, MONTH(created_at) as month, COUNT(type) as student_count,
count(case when student_level='subscription' then 1 end) as subscriptioncount,
count(case when student_level='pay_per_vedio' then 1 end) as pay_per_vediocount
FROM users
WHERE type = "student"
GROUP BY year, month
ORDER BY year, month
Please find below query for your need. 请在下面查询您的需要。 I hope it will execute as you wish.
我希望它将按您的意愿执行。
SELECT YEAR(created_at) as year, MONTH(created_at) as month, student_level,
COUNT(type) as student_count
FROM users
WHERE type = "student"
GROUP BY year, month, student_level
ORDER BY year, month, student_level
I now need to add 2 more columns, 'subscription' & 'pay_per_video' which will further divide the total count for each month and year into student level type either 'subscription' OR 'pay_per_video'.
现在,我需要再添加2列“订阅”和“ pay_per_video”,这将把每个月和每年的总数进一步划分为学生级别类型的“订阅”或“ pay_per_video”。
Here I will suggest you to just add a single column named something like level_type, that will store either subscription OR 'pay_per_video' as value. 在这里,我建议您仅添加一个名为level_type之类的列,该列将存储订阅或“ pay_per_video”作为值。
by doing this your SQL will be optimized to, 通过这样做,您的SQL将得到优化,
SELECT year, month, COUNT(student_count), level_type FROM (SELECT YEAR(created_at) as year, MONTH(created_at) as month, COUNT(type) as student_count
FROM users
WHERE type = "student"
GROUP BY year, month
ORDER BY year, month) as s
GROUP BY level_type
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.