[英]SQL count of different values in one column by date
I have a table that looks like: 我有一张表看起来像:
os date
-- ---
ios 2014-04-35 21:33:33
android 2014-04-35 21:33:33
ios 2014-04-35 21:33:33
ios 2014-04-35 21:33:33
I want to get an output to be the counts of ios/android in each month. 我想得到一个输出是每个月的ios / android计数。 So like:
所以喜欢:
Year Month ios android
2015 01 20 100
2015 02 400 20
Something like that. 这样的事情。
This is what I have right now. 这就是我现在所拥有的。 It's not quite right though:
虽然不太对劲:
SELECT year(last_modified_date) as cy
, month(last_modified_date) as cm
, (SELECT COUNT(*) FROM device_info WHERE os='ios')
, (SELECT COUNT(*) FROM device_info WHERE os='android')
FROM device_info
WHERE year(last_modified_date) IN (2014,2015)
GROUP BY year(last_modified_date)
, month(last_modified_date);
Tru this: 事实是,实际上,事实上:
SELECT YEAR(di.date) AS 'Year',
MONTH(di.date) AS 'Month',
SUM(di.os = 'ios') AS 'ios',
SUM(di.os = 'android') AS 'android'
FROM device_info AS di
GROUP BY YEAR(di.date), MONTH(di.date);
You can use conditional aggregation with case
expression: 您可以使用条件聚集
case
表达式:
select year(`last_modified_date`) as `Year`
, month(`last_modified_date`) as `Month`
, sum(`os` = 'ios') as `ios`
, sum(`os` = 'android') as `android`
from `device_info`
where year(`last_modified_date`) in (2014, 2015)
group by year(`last_modified_date`)
, month(`last_modified_date`);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.