Skip to content
Advertisement

SQLite: Selecting multiple counts from one column

I am running a query with SQLite and am relatively new to this. Glance of my dataset:

I want group the results by Col A and then show 3 more columns (mild, moderate, high) and their counts — Here is what I’m looking for:

I know how I can select just one of the columns (say, mild), but not sure how to select the others. Thanks!

Advertisement

Answer

You can pivot with conditional aggregation:

Demo on DB Fiddle:

ColA    | mild | moderate | high
:------ | :--- | :------- | :---
Atlanta | 1    | 2        | 0   
Houston | 0    | 2        | 0   
Miami   | 1    | 0        | 1   
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement