Last active
February 14, 2023 07:17
-
-
Save Charmatzis/4c87b8a4941e2ebddb63a29a34e04b3f to your computer and use it in GitHub Desktop.
Count for null and not null columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select count(a.id), sum(case when b.id is null then 0 else 1 end) as b_exists, | |
sum(case when b.id is null then 1 else 0 end) as b_does_not_exist | |
from a left join b on a.b_id = b.id; | |
-- Or even better | |
select count(a.id), | |
count(b.id) filter b.id is not null as b_exists, | |
count(b.id) filter b.id is null as b_does_not_exist | |
from a left join b on a.b_id = b.id; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment