1

I would like to combine the following two queries, so that I end up with three columns: RelativePath, field = null/empty string, field with value. I can execute the queries individually, but I'm having trouble combining them.

SELECT RELATIVEPATH, COUNT(RELATIVEPATH) FROM APP 
       WHERE (FIELD IS NULL) OR (FIELD = '')
       GROUP BY (RELATIVEPATH);

SELECT RELATIVEPATH, COUNT(RELATIVEPATH) FROM APP
       WHERE (FIELD IS NOT NULL) 
       GROUP BY (RELATIVEPATH);

1 Answer 1

7

You need the case statement:

SELECT RELATIVEPATH, sum(case when (FIELD IS NULL) OR (FIELD = '') then 1 else 0 end),
       sum(case when field is not null then 1 else 0 end)
FROM APP 
GROUP BY (RELATIVEPATH);

This is assuming that the intention of count(RelativePath) is to count all the rows. If you really want to count the non-NULL values of the column, you would use:

SELECT RELATIVEPATH, count(case when (FIELD IS NULL) OR (FIELD = '') then RelativePath end),
       count(case when field is not null then RelativePath end)
FROM APP 
GROUP BY (RELATIVEPATH);
Sign up to request clarification or add additional context in comments.

1 Comment

Probably need an "AND FIELD <> '' " in that 2nd CASE statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.