Column count based on a condition Column count based on a condition oracle oracle

Column count based on a condition


SELECT name, velocity, COUNT(*) AS count, COUNT(CASE WHEN category = 'A' AND id = 1 THEN 1 END)FROM section GROUP BY name, velocity

This should work.

If record does not meet the condition then it will return a NULL, and count skips NULL fields.


Something like this:

SELECT name, velocity, COUNT(*) AS count, SUM(CASE WHEN category = 'A' AND id = 1 THEN 1 ELSE 0 END)FROM section GROUP BY name, velocity


SELECT name, velocity, COUNT(*) AS count, category,        (select count(distinct name) from section where category = 'A' and id = 1)FROM section GROUP BY name, velocity