Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Pivot Query- need help


Pivot Query- need help

Author
Message
sammm
sammm
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 368
HI All,

I am trying to convert rows to columns using Pivot function. Here is my case

SELECT Month(a.createddate) as month,
count(statusid) as count,b.code
from ServiceRequest a,
StdActivity b
where a.createddate between '2009-06-01 00:00:00.000' and '2009-12-30 23:59:59.999'
--and statusid=10
and a.SRSetnumber like '%DELI%'
and a.statusid=b.StdActivityID
group by Month(a.createddate),a.statusid ,b.code
order by Month(a.createddate) ,a.statusid,b.code

Result Set :

month count code
6 8425 DELIVERY
6 20 CANCELLED


i want to use pivot function and convert the result set to


month delivery cancelled
6 8425
6 20



Any help would be greatly appreciated. Writing a reporting query and got stuck up with this .I havent used the pivot before and the help topics is little bit confusing to me.

Thanks
GSquared
GSquared
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20361 Visits: 9730
I'll say the same thing I always say: Do pivoting in the front end, not in the database. Easier, more efficient, gives the end user more options.

However, if you have to do it in the database for some reason, do you mean the results to be the way you presented them? It looks to me like the 20 should be in the last column and there should only be one row. Is that correct?

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
LutzM
LutzM
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9223 Visits: 13559
Thanks to Gail for stopping me to post in the wrong thread just in time!!

And here's what I was about to post "over there":
(Note: I second Gus preferring to do it on the app side though...)
If it needs to be dynamic please have a look at the DynamicCrossTab article referenced in my signature.

DECLARE @tbl TABLE
([MONTH] INT, [COUNT] INT, code VARCHAR(30)
)
INSERT INTO @tbl
SELECT 6, 8425, 'DELIVERY' UNION ALL
SELECT 6, 20, 'CANCELLED'

-- option 1: PIVOT
SELECT [MONTH],[DELIVERY], [CANCELLED]
FROM
(
SELECT [MONTH] , [COUNT] , code
FROM @tbl
) p
PIVOT
(
SUM ([COUNT])
FOR code IN ( [DELIVERY], [CANCELLED])
) AS pvt

-- option 2: "classic" CASE statement
SELECT
[MONTH],
SUM(CASE WHEN code ='DELIVERY' THEN [COUNT] ELSE 0 END) AS [DELIVERY],
SUM(CASE WHEN code ='CANCELLED' THEN [COUNT] ELSE 0 END) AS [CANCELLED]
FROM @tbl
GROUP BY [MONTH]





Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
sammm
sammm
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 368
Thank you very much for the script and your thoughts.:-).The script works fine.However I will check if i can work on the front end rather than in back end.

Thanks Again.
LutzM
LutzM
SSCrazy Eights
SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)SSCrazy Eights (9.2K reputation)

Group: General Forum Members
Points: 9223 Visits: 13559
Glad I could help :-)
Come back to this site if you need further assistance.
But remember: one post is better than 6! ;-)



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
sammm
sammm
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 368
Sure.I understand. I am sorry abt that. Thanks!
polkadot
polkadot
SSC Eights!
SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)SSC Eights! (869 reputation)

Group: General Forum Members
Points: 869 Visits: 1107
LutzM, thanks for the queries. Very nice learning experience.
Please elaborate, if you can, when to push back to front end. I have found it difficult to know when I should draw the line and say 'listen, I've sent you the data you need, now use the plethora of Excel data analysis nonmenclature, to pivot and to do your analysis on the report'. I found myself doing the trimming, the pivotting, the everything so that all the person on the excel end had to do is Insert Pivot Table and insert the fields into columns, rows, and filters.
General guidelines based on your experience, for pushing back on doing so much formatting on SQL end would be really appreciated.
valeryk2000
valeryk2000
SSC Veteran
SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)SSC Veteran (281 reputation)

Group: General Forum Members
Points: 281 Visits: 412
In my experience pivot codes work slower with big tables than case ones.
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