Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Pivots Expand / Collapse
Author
Message
Posted Friday, December 27, 2013 9:17 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, February 24, 2014 11:06 AM
Points: 109, 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
Post #1526191
Posted Friday, December 27, 2013 9:35 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:50 AM
Points: 3,360, Visits: 7,273
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1526201
Posted Friday, December 27, 2013 9:44 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, February 24, 2014 11:06 AM
Points: 109, 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]))
Post #1526207
Posted Friday, December 27, 2013 9:56 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:50 AM
Points: 3,360, Visits: 7,273
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.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1526212
Posted Monday, December 30, 2013 11:44 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, February 24, 2014 11:06 AM
Points: 109, Visits: 215
Thanks Luis, this actually worked. I really appreciate your help.
Post #1526567
Posted Monday, December 30, 2013 12:29 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 5:50 AM
Points: 3,360, Visits: 7,273
You're welcome.
Do you understand why did it work? You need to understand it to make any further modifications.



Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1526586
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse