Skip to content

How to add group by clauses based on different conditions?

I have a database like this : The first column is the name of the city. A, B and C are the columns with binary data 1 for yes and 0 for no.

(Database Name – D1)

I want to group these cities and have columns giving me counts of 1’s in column A, B, and C like below

The following code will give me the first column. Is there a way I can get all the three columns using a single query by using group by?

Advertisement

Answer

If you need to use count (to return a 0 if null values are present) then you can use a case statement within the count function to achieve this.

enter image description here

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