Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle - return multiple counts as one query

Tags:

sql

oracle

I have a couple of queries, detailed below. I'd like to be able to run one SQL query which returns both counts, is this possible?

1.

select nvl(count(rowid), 0) from tablename where OPP = 'FOO' and date = 'BAZ';

2.

select nvl(count(rowid), 0) from tablename where OPP = 'BAR' and date = 'BAZ';

I've only found MSSQL specific solutions in my searches so far.

like image 352
Raoul Avatar asked Jun 15 '11 10:06

Raoul


Video Answer


1 Answers

If you need them in a single row:

SELECT
    COUNT(CASE OPP WHEN 'FOO' THEN 1 END),
    COUNT(CASE OPP WHEN 'BAR' THEN 1 END)
FROM tablename
WHERE OPP IN ('FOO', 'BAR') AND date = 'BAZ'

(The GROUP BY approach by Thilo is a better generic solution anyway.)

Edit: I've removed NVL(). I had forgotten why I never use it.

like image 84
Álvaro González Avatar answered Sep 19 '22 13:09

Álvaro González