I have created an oracle query like as shown below,the query is working fine but the problem is that I want one more column which is the count of name where category should be A and id should be 1
SELECT name, velocity, COUNT(*) AS count, category FROM section GROUP BY name, velocity
Can anyone please tell me some solution for this
The Excel COUNTIFS function returns the count of cells that meet one or more criteria. COUNTIFS can be used with criteria based on dates, numbers, text, and other conditions.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With