Skip to content
Advertisement

Query to find the count of total IDs, and IDs having null in any column and the percentage of the two (i.e. Count of ID having null /total ID count)

There are two tables. Tables A has following structure

ID Flag Name
1X 1 Y
2Y 0 Null
3Z 1 Null
4A 1 Y

Table B has the following structure

B_ID City State
1X Y Null
2Y Null Null
3Z Null Y
4A Y Y

I want to get the count of all the IDs and the count of IDs that have Null in any of the columns (name, city, state), for e.g from the tables above only the ID 4A has non null value in all the three columns across both the tables, so the output should be like

Total_Count Ids having null Percentage missing
4 3 0.75%

Total_count is 4 as there are total of four IDs, ID having NULL is 3 because there are 3 IDs that have null in any of the three columns (viz. name,city,state), and Percentage missing is just IDs having null / Total_Count.

I tried using a query along the following lines

But the query is not returning the desired output, can you please suggest me a better way? Thank You

Advertisement

Answer

Use conditional aggregation:

See the demo.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement