Skip to content
Advertisement

COUNT Multiple Columns using GROUP BY – SQL

I am trying to create a GROUP BY COUNT of ~30 columns in my database. The database is basically a shiftplan, where on each column a row can be assigned different shift types (denoted as a D, N, A, X, F, C, etc..).

I can use the following query to get a count of each shift type, am struggling to work out the best way to copy this across 30 more columns:

This gives me the output:

Which is close to what I want, I just want to carry it across to all other 30 workdays and end up with something more like this:

Thanks a lot for any advice you can give!

Advertisement

Answer

I think that you want a cross join with a fixed list of values, then conditional aggregation:

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