对于我正在建设的报告工具,我希望能够计算出多少"事件"到目前为止已经记录的每个国家都有这个问题:
SELECT 'country',
COUNT(*) AS total,
SUM(CASE WHEN `country` = 'USA' THEN 1 ELSE 0 END) AS USCount,
SUM(CASE WHEN `country` = 'Canada' THEN 1 ELSE 0 END) AS CanadaCount
FROM events WHERE `date` > '2015-06-01' AND `date` < '2015-08-01'
唯一的问题是,当我开始将其他国家添加到组合中时,案例将开始变得庞大。有没有办法让我可以遍布各个国家/地区来检索每个国家/地区的所有各种计数?感谢。
答案 0 :(得分:2)
尝试以下SQL语句:
Select country, count (*)
From events
Group by country
答案 1 :(得分:2)
按国家/地区名称对其进行分组以明智地计算国家/地区
SELECT country,
COUNT(*) AS total
FROM events WHERE `date` > '2015-06-01' AND `date` < '2015-08-01' group by country;