原始查询如下:
SELECT
id
,COUNT(A_NO) AS count
FROM table1
WHERE date = '2022-02-02'
AND p_no IS NOT NULL
GROUP BY id;
p_no IS NOT NULL
我需要通过不使用WHERE 子句中的条件来重写上述查询。所以我尝试了以下 2 个查询,但它们需要很长时间才能执行
SELECT
id
,SUM(CASE WHEN A_NO IS NOT NULL AND p_no IS NOT NULL THEN 1 ELSE 0 END) AS count
FROM table1
WHERE date = '2022-02-02'
GROUP BY id;
SELECT
id
,COUNT_IF(A_NO IS NOT NULL AND p_no IS NOT NULL) AS count
FROM table1
WHERE date = '2022-02-02'
GROUP BY id;
有没有其他方法可以重写 SQL 查询?