Skip to content
Advertisement

SQL counting in table

I’ve got a table that has a name, and a month in.

What would the SQL look like to make a table, that counts how many entries there is per month against each name. e.g. enter image description here

Im confident to modify the vb.net code to reflect the actual needs of the project and to get it into a DGV, its just the SQL that i’m a bit iffy with, but this is the easiest way for me to explain what i’m trying to achieve.

Any help would be appreciated.

Cheers, Pete

Advertisement

Answer

You can use conditional aggregation. Something like this:

select name,
       sum(case when month(date) = 1 then 1 else 0 end) as month_1,
       sum(case when month(date) = 2 then 1 else 0 end) as month_2,
       . . .
from t
group by name;
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement