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


How to pivot using pivot clause of sqlserver 2005 for three values


How to pivot using pivot clause of sqlserver 2005 for three values

Author
Message
rajemessage 14195
rajemessage 14195
SSChasing Mays
SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)SSChasing Mays (634 reputation)

Group: General Forum Members
Points: 634 Visits: 366
Dear all

Is it possible to formulate the query using pivote clause of sqlserver 2005.

( Table name is) BridgeHawra

--------

ReportDate, , Pole, DiameterCost,Flag,OuterDiaCost

01 jan 2014, ,1 , 4.5 ,yes,6

01 jan 2014, ,2 , 4.5 ,no,6

01 jan 2014, ,3 , 5.5 ,no,6

02 jan 2014, ,4 , 5.5 ,no,5.9

02 jan 2014, ,5 , 5.5 ,no,6

1)Pole Col will have unique value.

o2)One pole will have one DiameterCost, but n number of DiameterCost can be there.

I have to show a report which should show , count of poles and cost(DiameterCost* pole count of the day of each DiameterCost) for each date and DiameterCost.

as following


DateOfReport , PoleCountofDiameterCost4.5, DiameterCost4.5, PoleCountOfDiameterCost5.5,DiameterCost5.5

01 jan 2014 , 2 ,(2*4.5) ,1 ,(1*5.5)

02 jan 2014 , 0 ,0 ,2 ,(2*5.5)


yours sincerely
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62317 Visits: 17954
Yes you have to use a cross tab. If you know the number of elements you can use a static cross tab. If the number of elements is not known it will have to be the dynamic version. You can read about them by following the links in my signature. You might also take a look at the first link in my signature for best practices when posting questions.

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 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