SQL Filter
Standard ANSI SQL
- partially aggregate data
WHERE
clause for aggregate data- vs
HAVING
- vs
SELECT
COUNT(*) AS unfiltered,
COUNT(*) FILTER (WHERE i < 5) AS sub_five,
AVG(i) FILTER (WHERE i < 5) AS avg_sub_five,
AVG(CASE
WHEN i < 5 THEN 0
WHEN i >= 5 THEN 1
ELSE NULL -- implicit ELSE
END) AS sub_five_case
FROM GENERATE_SERIES(1,10) AS s(i);
[!Query result]-
Filter vs Group By and Having¶
WITH sales(rep_id, territory, earnings) AS (
VALUES (1, 'A', 10),
(1, 'B', 20),
(2, 'C', 30),
(2, 'D', 40),
(3, 'B', 100),
(4, 'B', 0) -- gets filtered out by the HAVING
)
SELECT
rep_id,
SUM(earnings) AS earnings,
SUM(earnings) FILTER (WHERE territory != 'B') AS earnings_without_b,
SUM(earnings) FILTER (WHERE territory != 'D') AS earnings_without_d
FROM sales
GROUP BY rep_id
HAVING SUM(earnings) > 0;
[!Query with FILTER and GROUP BY result]-
GROUP BY
andHAVING
apply first- then
FILTER
takes effect after - you don't need
GROUP BY
to useFILTER
FILTER
does an aggregation on a filtered subset of the queries
Last update:
2023-04-24