December 27, 2013 at 9:17 am
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
December 27, 2013 at 9:35 am
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
December 27, 2013 at 9:44 am
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]))
December 27, 2013 at 9:56 am
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
December 30, 2013 at 11:44 am
Thanks Luis, this actually worked. I really appreciate your help.
December 30, 2013 at 12:29 pm
You're welcome.
Do you understand why did it work? You need to understand it to make any further modifications.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply