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.
Author |
Topic |
Skydolphin
Starting Member
43 Posts |
Posted - 2006-12-11 : 13:55:03
|
Hi all, I need to build a query that will return counts on several groupings. We have an employee table that contains not only the employee namebut also their location (Countrycode) and department (departmentdode). Based on a month ending date, I need to find out how many employees we have for eachcountry broken out by department. The end result should look something like this. Month Ending: 11/30/2006 CountryCode 30 20 40 50 60 TotalDepartment Sales 30 3 3 2 18 56Professional Services 5 10 4 3 9 31Marketing 4 4 0 0 2 10Product Development 3 56 0 0 0 59Operations- Support 5 2 0 0 5 12General & Administrative 9 8 2 0 4 23 I am not a SQL expert and I'm having some difficulty figuring this one out.Can someone please point me in the right direction?Thanks, RhondaRhonda |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-12-11 : 14:08:18
|
SELECT DepartmentCode,SUM(CASE WHEN CountryCode = 30 THEN 1 ELSE 0 END) AS '30',SUM(CASE WHEN CountryCode = 20 THEN 1 ELSE 0 END) AS '20',SUM(CASE WHEN CountryCode = 40 THEN 1 ELSE 0 END) AS '40',SUM(CASE WHEN CountryCode = 50 THEN 1 ELSE 0 END) AS '50',SUM(CASE WHEN CountryCode = 60 THEN 1 ELSE 0 END) AS '60',SUM(1) AS 'Total'FROM YourTableNameHereWHERE SomeDateTimeColumnHere >= '20061201' AND SomeDateTimeColumnHere < '20070101'GROUP BY DepartmentCodePeter LarssonHelsingborg, SwedenEDIT: Changed CountryCode = X |
|
|
Skydolphin
Starting Member
43 Posts |
Posted - 2006-12-11 : 14:13:42
|
Thanks Peter,I was so close earlier and got frustrated and gave up. This is something I can work with.RhondaRhonda |
|
|
|
|
|