Skip to content
Advertisement

Counting number of records with matching values in separate fields

database/SQL novice here. I have a table in DB MS SQL Server 2008 that looks like this:

number of request,      employee,   parameter1, parameter2, parameter3
ID1139151   employee1   1   1   0
ID1139152   employee1   0   0   1
ID1139153   employee1   0   1   0
ID1139154   employee2   0   0   1
ID1139155   employee2   0   1   1
ID1139156   employee2   1   1   0
ID1139157   employee3   0   1   0
ID1139158   employee3   0   1   0

I want to find number of rows by values parameter1, parameter2, parameter3, number of request

Result should be like this:

SUMM number of request,     employee,   SUMM parameter1,    SUMM parameter2,    SUMM parameter3
3   employee1   1   2   1
3   employee2   1   2   2
2   employee3   0   2   0

How can I do it? Please help me.

Advertisement

Answer

select count([number of request]),
       employee,
       sum(parameter1) as sum_p1,
       sum(parameter2) as sum_p2,
       sum(parameter3) as sum_p3
from your_table
group by employee
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement