我有两个选择查询:
SELECT MONTH(date) AS month, count(DISTINCT ip)
FROM table_name WHERE field = 1 GROUP BY month
和
SELECT MONTH(date) AS month, count(DISTINCT ip)
FROM table_name WHERE field = 2 GROUP BY month
如何编写一个查询来选择
SELECT MONTH(date) AS month,
count(DISTINCT ip) [ for field = 1],
count(DISTINCT ip) [ for field = 2]
最佳答案
SELECT MONTH(date) AS month,
count(DISTINCT case when field = 1 then ip end) as f1,
count(DISTINCT case when field = 2 then ip end) as f2
FROM table_name
WHERE field in (1,2)
GROUP BY month
关于sql - 选择同一列上的两个计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3830528/