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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help in a query
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Starting Member

8 Posts

Posted - 07/30/2007 :  08:34:03  Show Profile  Reply with Quote
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)

Patron Saint of Lost Yaks

30421 Posts

Posted - 07/30/2007 :  08:46:49  Show Profile  Visit SwePeso's Homepage  Reply with Quote
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"

Edited by - SwePeso on 07/30/2007 08:47:54
Go to Top of Page

Premature Yak Congratulator

22864 Posts

Posted - 07/30/2007 :  10:14:17  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Also read about Cross-tab reports in sql server help file


Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000