SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Adding GROUP BY to PIVOT operator (how?)


Adding GROUP BY to PIVOT operator (how?)

Author
Message
polkadot
polkadot
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2988 Visits: 1257
I am trying to show how many employees were hired in each department by startyear (2001, 2002, 2003) and then to rollup these year totals by department. So, for example, Engineering hired one employee in 2001, another in 2002, and another in 2003 with 3 total in the Engineering department.
I am able to get the first part of this problem solved with a PIVOT (showing 1 employee under each year) but I'm unable to add a GROUP BY clause after the PIVOT that will rollup each years total by department to show total number (3) of employees hired by department (eg. Enginneering).
Is GROUP BY the solution or is there another way to roll up by DeptName?
---this works:
select DeptName, [2001], [2002], [2003]
from
(select DeptName, EmpID, year(StartDate) as startyear
from StaffHistory
) as p
PIVOT (count(empid) for Startyear in ([2001], [2002], [2003])) as pvt

---this doesn't
select DeptName, [2001], [2002], [2003]
from
(select DeptName, EmpID, year(StartDate) as startyear
from StaffHistory
) as p
PIVOT (count(empid) for Startyear in ([2001], [2002], [2003])) as pvt
GROUP BY DeptName


CREATE TABLE StaffHistory
(DeptName varchar(40)NOT NULL, EmpID int NOT NULL, StartDate datetime NOT NULL, EndDate datetime NULL);
INSERT INTO StaffHistory
VALUES
('Engineering', 1, '2003-12-16 00:00:00.000', null),
('Tool Design', 2, '2003-07-01 00:00:00.000', null),
('Sales', 3, '2003-11-01 00:00:00.000', null),
('Engineering', 4, '2002-07-01 00:00:00.000', null),
('Tool Design', 5, '2002-07-01 00:00:00.000', null),
('Sales', 6, '2002-11-01 00:00:00.000', null),
('Engineering', 7, '2001-07-01 00:00:00.000', null),
('Tool Design', 8, '2001-07-01 00:00:00.000', null),
('Sales', 9, '2001-11-01 00:00:00.000', null);


--Quote me
Sam Vanga
Sam Vanga
Old Hand
Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)

Group: General Forum Members
Points: 364 Visits: 502
Try this, it will give you a start.

SELECT DeptName
, [2001]
, [2002]
, [2003]
, [All]
FROM (
SELECT DeptName
, startyear = isnull(cast(year(StartDate) AS CHAR(4)), 'All')
, Cnt = Count(empid)
FROM StaffHistory
GROUP BY DeptName
, year(StartDate)
WITH rollup
) src
pivot(sum(Cnt) FOR startyear IN (
[2001]
, [2002]
, [2003]
, [All]
)) pvt
WHERE DeptName IS NOT NULL



Sam Vanga
http://SamuelVanga.com
polkadot
polkadot
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2988 Visits: 1257
This solution works, Thanks Sam.
I have a question about this line:
, startyear = isnull(cast(year(StartDate) AS CHAR(4)), 'All')
I understand ISNULL works because a NULL for startdate is created by the creation of an All column. But, can you explain why it is that you have to use the CAST function, as in , "cast(year(StartDate) AS CHAR(4)"? Is it because datetime is not convertible to a string value?

--Quote me
Sam Vanga
Sam Vanga
Old Hand
Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)Old Hand (364 reputation)

Group: General Forum Members
Points: 364 Visits: 502
Look at the remarks section here: http://msdn.microsoft.com/en-us/library/ms184325.aspx

When using ISNULL, check and replacement expressions should be of compatible data types.

Sam Vanga
http://SamuelVanga.com
Luis Cazares
Luis Cazares
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40696 Visits: 19815
For general knowledge and maybe performance (you'll have to test your case), I leave you with my personal preference:

SELECT DeptName,
SUM( CASE WHEN YEAR( StartDate) = 2001 THEN 1 ELSE 0 END) [2001],
SUM( CASE WHEN YEAR( StartDate) = 2002 THEN 1 ELSE 0 END) [2002],
SUM( CASE WHEN YEAR( StartDate) = 2003 THEN 1 ELSE 0 END) [2003],
COUNT(*) [All]
FROM StaffHistory
GROUP BY DeptName



You can find more information here:
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search