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


Pivots


Pivots

Author
Message
DaveDB
DaveDB
SSC-Enthusiastic
SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)

Group: General Forum Members
Points: 155 Visits: 215
Happy holidays!

I am having issues using PIVOT. I have never seemed to truly understand the syntax behind it and I know this is a simple question....

I have a simple table that has 3 columns: DayofWeek (Mon - Fri), Time(8:00AM - 5:00PM for each day), and CallsOffered (This is the aggregate). I just need to pivot on the DayofWeek, so I can have Mon - Friday as column names and I cannot seem to do it correctly.

The table looks like this...

Monday - 8:30AM - $1254.52
Monday - 9:00AM - $546.32
Tuesday - 8:30AM - $5649.32
Tuesday - 9:00AM - $654.00
Wednesday - 9:00AM - $546.32
Wednesday - 9:00AM - $546.32
Thursday - 9:00AM - $546.32
Thursday- 9:00AM - $546.32
Friday - 9:00AM - $546.32
Friday - 9:00AM - $546.32


Any help would be greatly appreciated!

Dave
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16262 Visits: 19059
You didn't need to open a new thread. You could just post this on the previous one.
Here's an example, but don't blame me if it doesn't work as you want because you continue to avoid our advices.


SELECT Time,
SUM( CASE WHEN DayOfWeek = 'Monday' THEN CallsOffered END) AS Monday,
SUM( CASE WHEN DayOfWeek = 'Tuesday' THEN CallsOffered END) AS Tuesday
FROM SomeTable
GROUP BY Time




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
DaveDB
DaveDB
SSC-Enthusiastic
SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)

Group: General Forum Members
Points: 155 Visits: 215
Luiz,

Pardon my angst today. I have been trying for weeks to get a simple PIVOT example and nothing is working. I have been on multiple posts, but cannot seem to get this working. Could you please take a quick look at the syntax below and let me know if this looks correct to you.

Thanks for all your help and apologies for my mood.


select [0] AS Monday,
[1] AS Tuesday,
[2] AS Wednesday,
[3] AS Thursday,
[4] AS Friday
from (select * from #MnthCustSvcAVGCallsOfferedByINT) as src
PIVOT (sum(AVGCallOffered) FOR Interval IN ([0],[1],[2],[3],[4]))
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16262 Visits: 19059
If you have problems with pivot, you should try the method I posted earlier. It's even better when you need to pivot several columns. You'll find information in the link I posted and you rejected.
You had some problems with your code but in order to get to it, I needed to transform your sample data to a consumable way and you should do this in the future. Here's the example:

CREATE TABLE #Sample(
[DayofWeek] varchar(10), [Time] varchar(10), CallsOffered money)
INSERT #Sample SELECT
'Monday' , '8:30AM' , $1254.52 UNION ALL SELECT
'Monday' , '9:00AM' , $546.32 UNION ALL SELECT
'Tuesday' , '8:30AM' , $5649.32 UNION ALL SELECT
'Tuesday' , '9:00AM' , $654.00 UNION ALL SELECT
'Wednesday' , '9:00AM' , $546.32 UNION ALL SELECT
'Wednesday' , '9:00AM' , $546.32 UNION ALL SELECT
'Thursday' , '9:00AM' , $546.32 UNION ALL SELECT
'Thursday', '9:00AM' , $546.32 UNION ALL SELECT
'Friday' , '9:00AM' , $546.32 UNION ALL SELECT
'Friday' , '9:00AM' , $546.32


With this sample data, the rest is easier.

select [Monday],[Tuesday],[Wednesday],[Thursday],[Friday] --The column names are the values used below
from (select * from #Sample) as src
PIVOT (sum(CallsOffered) FOR [DayofWeek]
IN ([Monday],[Tuesday],[Wednesday],[Thursday],[Friday]) --You need to include actual values here
)Piv --You need to alias the pivot result




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
DaveDB
DaveDB
SSC-Enthusiastic
SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)SSC-Enthusiastic (155 reputation)

Group: General Forum Members
Points: 155 Visits: 215
Thanks Luis, this actually worked. I really appreciate your help.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16262 Visits: 19059
You're welcome.
Do you understand why did it work? You need to understand it to make any further modifications.


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