我有两个执行相同操作的请求,但针对不同的组。
SELECT count() FROM Table1 WHERE label = 'label1' AND group IN (0) AND date >= '2018-01-01' AND date <= '2018-02-01';
SELECT count() FROM Table1 WHERE label = 'label1' AND group IN (3, 4, 5, 6, 7, 8) AND date >= '2018-03-01' AND date <= '2018-03-01';
我可以将其拆分为一个查询吗?
答案 0 :(得分:2)
可以使用。.
UNION ALL
在两个查询之间..仅返回一个结果。仍然是两个查询,但是,如果您希望它成为一个查询,则可以使用CASE WHEN。
SELECT
SUM(CASE WHEN group = 0 AND date BETWEEN '2018-01-01' AND '2018-02-01' THEN 1 END) As Group1,
SUM(CASE WHEN group IN (3, 4, 5, 6, 7, 8) AND date BETWEEN '2018-03-01' AND '2018-03-01 THEN 1 END) As Group2
FROM Table1
WHERE label = 'label1'