June 29, 2010 at 3:07 pm
Hi all,
I was wondering if someone could help me out on a pivot here, I've done it before but am totally drawing a blank
I have data like this:
ShipDate, Qty, Department
06/28/2010, 100, 'Sanding'
06/28/2010, 150, 'Waxing'
06/28/2010, 130, 'Finishing'
06/28/2010, 82, 'Sanding'
07/05/2010, 24, 'Sanding'
07/05/2010, 45, 'Waxing'
07/05/2010, 12, 'Finishing'
07/12/2010, 100, 'Sanding'
07/12/2010, 84, 'Waxing'
What I want to get to is something like:
Shipdate, Sanding, Waxing, Finishing
06/28/2010, 182, 150, 130
07/05/2010, 24, 45, 12
07/12/2010, 100, 84, 0
I think I can use a PIVOT, but I can not for the life of me get it working..
any help would be greatly appreciated.
Thanks
--------------------------
I long for a job where my databases dont have any pesky users accessing them 🙂
June 29, 2010 at 3:23 pm
Here's a code snippet that should get you started.
If you have variable Departments you might want to consider using the DynamicCrossTab approach. If you're not familiar with it you should start with the CrossTab article refernenced in my signature (modify it until you get the same result as with PIVOT) followed by reading the DynamicCrossTabArticle. At the end you'll have a really flexible and well performing solution.
Or, as a totally different approach, you could do the pivoting at the frontend side... 😉
DECLARE @tbl TABLE
(
ShipDate DATETIME, Qty INT,Department VARCHAR(30)
)
INSERT INTO @tbl
SELECT '06/28/2010', 100, 'Sanding' UNION ALL
SELECT '06/28/2010', 150, 'Waxing' UNION ALL
SELECT '06/28/2010', 130, 'Finishing' UNION ALL
SELECT '06/28/2010', 82, 'Sanding' UNION ALL
SELECT '07/05/2010', 24, 'Sanding' UNION ALL
SELECT '07/05/2010', 45, 'Waxing' UNION ALL
SELECT '07/05/2010', 12, 'Finishing' UNION ALL
SELECT '07/12/2010', 100, 'Sanding' UNION ALL
SELECT '07/12/2010', 84, 'Waxing'
SELECT shipdate,Sanding, Waxing, Finishing
FROM
(
SELECT shipdate, department, SUM(qty) AS total
FROM @tbl
GROUP BY shipdate, department
) p
PIVOT
(
SUM (total)
FOR department IN ( Sanding, Waxing, Finishing )
) AS pvt
ORDER BY shipdate
June 29, 2010 at 3:43 pm
That worked perfectly, thanks so much..
I was pretty close in what I had but wasn't quite there.. and the link on dynamic cross tab sounds pretty interesting - i'll definitely be checking that out when i get done with some work..
Thanks again!
--------------------------
I long for a job where my databases dont have any pesky users accessing them 🙂
June 29, 2010 at 4:54 pm
torpkev (6/29/2010)
That worked perfectly, thanks so much..I was pretty close in what I had but wasn't quite there.. and the link on dynamic cross tab sounds pretty interesting - i'll definitely be checking that out when i get done with some work..
Thanks again!
Glad I could help!:-D
Especially if the links I pointed you at will add some benefit (even though those are just "borrowed" articles from Jeff Moden). So, all I could claim for is pointing you at a different (/ right?) direction. If it works, its all my donation. If it doesn't, it'll be all Jeffs fault. 😀 (not that I've seen it happen within the time I've been around here though... 😉 )
June 29, 2010 at 8:28 pm
lmu92 (6/29/2010)
torpkev (6/29/2010)
That worked perfectly, thanks so much..I was pretty close in what I had but wasn't quite there.. and the link on dynamic cross tab sounds pretty interesting - i'll definitely be checking that out when i get done with some work..
Thanks again!
Glad I could help!:-D
Especially if the links I pointed you at will add some benefit (even though those are just "borrowed" articles from Jeff Moden). So, all I could claim for is pointing you at a different (/ right?) direction. If it works, its all my donation. If it doesn't, it'll be all Jeffs fault. 😀 (not that I've seen it happen within the time I've been around here though... 😉 )
Sounds like you've got it all figured out. 😛 (Thanks for the compliment, Lutz)
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply