Click here to Skip to main content
15,914,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hii ,

I want department wise data ,

this is what i tried .. but i think i made something wrong in it . What i want is .. How many objectives in department IT , live , completed in IT section .. same for other departments


SQL
select distinct DepartmentName,TotalLive,TotalDraft,TotalCompleted
from
(
		select d.departmentName,(select count(*) from objectives where objective_Status=1) AS TotalLive,
		(select count(*) from objectives where objective_Status=2) AS TotalDraft,
		(select count(*) from objectives where objective_Status=3) AS TotalCompleted
		from Employee as e 
		inner join Departments as d
		on e.DepartmentId = d.DepartmentId
)a


What i want is

Department Name | Total Completed | Total Live | Total Draft

IT 35 40 45
Admin 55 39 41
Posted
Updated 29-Apr-15 4:20am
v3
Comments
Maciej Los 29-Apr-15 9:12am    
Please, improve your question and add sample data. Then add expected output. It would be easy to understand your issue.
Torakami 29-Apr-15 9:16am    
Hey , i hve just updated my query .. i think this would be much understandable what i want exactly
Maciej Los 29-Apr-15 9:27am    
I see your query. It isn't helpful. Post your data.

1 solution

Try:
SQL
SELECT d.departartmentName, 
       SUM(CASE WHEN e.objective_Status=1 THEN 1 ELSE 0 END) AS TotalLive,
       SUM(CASE WHEN e.objective_Status=2 THEN 1 ELSE 0 END) AS TotalDraft,
       SUM(CASE WHEN e.objective_Status=3 THEN 1 ELSE 0 END) AS TotalCompleted
FROM Employee e
JOIN Departments d
ON e.DepartmentId = d.DepartmentId
GROUP BY departmentName
 
Share this answer
 
Comments
Torakami 30-Apr-15 3:28am    
hey thanks brother , this helped to achieve what i wanted to .. Thanks
OriginalGriff 30-Apr-15 4:06am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900