TBogdan
TBogdan

Reputation: 737

Using Query on SQL Server database to count records of some type and grouped by some foreign key

I'm developing an ASP.NET web application using Visual Studio 2010 professional and a SQL Server database. I have some records in the database for the next tables: enter image description here

Is there a way to calculate for example with only one query, how many records in table sarcini has stare_task (means task_state) = closed according to each employee(angajat) foreign key in sarcini? I think this is too ambigous but I don't know how to explain exactly what I mean.

Upvotes: 1

Views: 307

Answers (1)

Brad
Brad

Reputation: 635

Select
    id_angajat
    ,sum(case when stare_task = 'Closed' then 1 else 0 end) as [Closed_Records]
From sarcini
Group by id_angajat

Assuming MS SQL Server and that you are just using table sarcini..

Edit: Sql Fiddle example: http://sqlfiddle.com/#!3/c2c89/2

Upvotes: 2

Related Questions