我有两个选择查询:
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]
答案 0 :(得分:5)
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