I have two queries that count the total of employees according to multiple conditions; the only thing that changes is the last two AND clauses; I don’t know how I can return the results in the same query.
First Query
SELECT
COUNT(*)
FROM
(
SELECT
E.NAME,
E.LAST_NAME,
E.BIRTH_DATE,
E.ID
FROM
EMPLOYEES E
WHERE E.BIRTH_DATE BETWEEN '2022-10-18 00:00:00' AND '2022-10-18 23:59:59'
AND E.NAME IS NOT NULL
AND E.LAST_NAME IS NOT NULL
GROUP BY E.NAME, E.LAST_NAME, E.BIRTH_DATE,E.ID
) AUX;
Second Query
SELECT
COUNT(*)
FROM
(
SELECT
E.NAME,
E.LAST_NAME,
E.BIRTH_DATE,
E.ID
FROM
EMPLOYEES E
WHERE E.BIRTH_DATE BETWEEN '2022-10-18 00:00:00' AND '2022-10-18 23:59:59'
AND E.NAME IS NULL
AND E.LAST_NAME IS NULL
GROUP BY E.NAME, E.LAST_NAME, E.BIRTH_DATE,E.ID
) AUX;
Expected output:
total |
---|
3 –first row |
5 –second row |
2
Answers
Quickest method (ie, least amount of re-writing) is simply
UNION
your queries together like this:You could also use conditional aggregation, which uses less real estate and quite easy to read, and produce output in columns with this….
Unlike a union, this does not require two separate queries. Only a single pass
through the result set would be required.
But you asked for the results on separate rows, so…
This makes the two results into an array and then unnests the result into
separate rows while still keeping the same query efficiency.