MYSQL:根据日期计算不同值的出现次数
MYSQL: Counting the occurences of distinct values depending on its date
我正在尝试查找 MYSQL 查询,该查询将在特定字段中查找不同的值,根据日期计算该值出现的次数。
示例数据库:
in_date item_code
--------- ---------
2015/11/25 item1
2015/11/25 item2
2015/12/10 item1
2015/12/10 item1
2015/12/10 item2
2016/01/01 item1
预期输出:
Date Item1 Item2
---------- ----- -----
2015/11/25 1 1
2015/12/10 2 1
2016/01/01 1 0
提前致谢!!
您可以使用条件聚合来做到这一点:
select date, sum(item_code = 'item1') as item1,
sum(item_code = 'item2') as item2
from t
group by date;
我正在尝试查找 MYSQL 查询,该查询将在特定字段中查找不同的值,根据日期计算该值出现的次数。
示例数据库:
in_date item_code
--------- ---------
2015/11/25 item1
2015/11/25 item2
2015/12/10 item1
2015/12/10 item1
2015/12/10 item2
2016/01/01 item1
预期输出:
Date Item1 Item2
---------- ----- -----
2015/11/25 1 1
2015/12/10 2 1
2016/01/01 1 0
提前致谢!!
您可以使用条件聚合来做到这一点:
select date, sum(item_code = 'item1') as item1,
sum(item_code = 'item2') as item2
from t
group by date;