Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help in a query

Author  Topic 

aashirwaads
Starting Member

8 Posts

Posted - 2007-07-30 : 08:34:03
Hi friends,

Please guide me as I am stuck in a query

I have got 3 queries

1.columns-employee_id,employee_name,count(sales role)
2.columns-employee_id,employee_name,count(employees who are clerks)
3.columns-employee_id,employee_name,count(employees who are not clerks)


now I want to join these three queries so that the output is shown as one table as below

employee_id,employee_name,count(sales role),count(employees who are clerks),count(employees who are not clerks)

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-30 : 08:46:49
Try something similar to this.

SELECT EmployeeID, EmployeeName,
COUNT(*) AS TotalRoles,
SUM(CASE WHEN Role = 'clerk' THEN 1 ELSE 0 END) AS Clerks,
SUM(CASE WHEN Role = 'clerk' THEN 0 ELSE 1 END) AS NotClerks
FROM Employees
GROUP BY EmployeeID, EmployeeName
ORDER BY EmployeeID, EmployeeName

E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-07-30 : 10:14:17
Also read about Cross-tab reports in sql server help file

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -