Sql query

Hi,
Here are the three table TableA,TableB, and TableC with following columns.
Table A
EmpId  Name
001        abc
002        bbc
Table B
Id  empid , date, statusId  (This is reference of Statusid from table C)
1    001     ,12-2-2015,   1
2    001  , 13-2-2015,     1
3    001  , 14-2-2015,     2
4    002  , 15-2-2015 ,     3
Table C
statusId  statusName
1      Inprogress
2      Completed
3      Not Open


Now what is the count of inprogress and what is the count of completed and what is the count of Not open for each employee.
Please provide the query.
praveen1981Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You can use the following query...

select tb1.name,tb3.statusname,count(tb2.id)
from table1 tb1
left join table2 tb2 on tb1.empid=tb2.empid
left join table tb3 on tb2.statusid=tb3.statusid

Open in new window


Saurabh...
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
SELECT A.Name C.StatusName, COUNT(B.StatusID)
FROM TableB B
    INNER JOIN TableC C ON B.StatusID = C.StatusID
    INNER JOIN TableA  A ON B.Empid = A.EmpId
GROUP BY A.Name, C.StatusName

Open in new window

0
Saurabh Singh TeotiaCommented:
Ahh Good catch vitor..I missed group by in my statement my bad...

select tb1.name,tb3.statusname,count(tb2.id)
from table1 tb1
left join table2 tb2 on tb1.empid=tb2.empid
left join table tb3 on tb2.statusid=tb3.statusid
group by tb1.name,tb3.statusname

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

praveen1981Author Commented:
Hi

I want the result as follows

EmpId  Inprogress Completed NotOpen

001           2                    1                0
002           0                    0                1

could you please suggest.
0
Saurabh Singh TeotiaCommented:
You can do something like this to do what you are looking for..

select tb1.empid,
'Inprogress'=sum(case tb3.statusname='Inprogress' then 1 else 0 end),
'Completed'=sum(case tb3.statusname='Completed' then 1 else 0 end),
'Not Open'=sum(case tb3.statusname='Not Open' then 1 else 0 end)
from tablea tb1
left join tableb tb2 on tb1.empid=tb2.empid
left join tablc tb3 on tb2.statusid=tb3.statusid
group by tb1.empid

Open in new window


Saurabh...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
You can use PIVOT table:
SELECT *
FROM (SELECT A.EmpId, C.StatusName, B.StatusID
		FROM TableB B
			INNER JOIN TableC C ON B.StatusID = C.StatusID
			INNER JOIN TableA  A ON B.Empid = A.EmpId) T
PIVOT
	(COUNT(StatusID) FOR StatusName IN (Inprogress, Completed, [Not Open])
) AS PivotTable

Open in new window

0
praveen1981Author Commented:
Thanks
0
Saurabh Singh TeotiaCommented:
Any particular reason for the grade b?? Because i'm assuming your problem was solved on this one properly..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.