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

How to split query result into seperate columns Expand / Collapse
Author
Message
Posted Monday, March 04, 2013 8:45 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, February 20, 2014 10:38 AM
Points: 40, Visits: 148
I have one table that contains all my data. I can group into a result as below (the weight would the the sum per day):

Time_____________________Plant ______Weight
01/03/2013 10:00___________AP1________1.2
02/03/2013 11:00___________AP1________2.3
03/03/2013 12:00 __________AP1 ________1.1
01/03/2013 10:00 __________AP2_________1.4
02/03/2013 10:00 __________AP2 ________1.1
03/03/2013 10:00 __________AP2_________1.1


How do I go about splitting the sum(weight) up into seperate columns based on 'Plant' so the query result looks like this:

Time_____________________AP1______AP2
01/03/2013 10:00__________1.2_______1.4
02/03/2013 11:00__________2.3_______1.1
03/03/2013 12:00__________1.1_______1.1

Basically I want to group the results into columns rather than one long result.

Thanks
Post #1426289
Posted Monday, March 04, 2013 9:01 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 11,927, Visits: 10,967
What you are referring to is called a cross tab or pivot. See the articles in my signature about those topics.

_______________________________________________________________

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 Moden's 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)
Post #1426299
Posted Monday, March 04, 2013 10:14 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: Yesterday @ 1:12 PM
Points: 3,297, Visits: 2,340
Thanks, Sean. Good article. You know, I'm consistently impressed by just how many different uses there are for the tally table. So simple in design, yet the performance gains are through the roof. Thanks again.


Tally Tables - Performance Personified
Best practices on how to ask questions
Post #1426353
Posted Monday, March 04, 2013 10:16 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 11,927, Visits: 10,967
Ed Wagner (3/4/2013)
Thanks, Sean. Good article. You know, I'm consistently impressed by just how many different uses there are for the tally table. So simple in design, yet the performance gains are through the roof. Thanks again.


One of the regulars around here (I always forget who) once called it the "Swiss Army Knife of SQL". They were absolutely correct. The usage of something so simple is truly amazing.


_______________________________________________________________

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 Moden's 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)
Post #1426354
Posted Monday, March 04, 2013 4:49 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 5:41 PM
Points: 35,944, Visits: 30,229
Sean Lange (3/4/2013)
Ed Wagner (3/4/2013)
Thanks, Sean. Good article. You know, I'm consistently impressed by just how many different uses there are for the tally table. So simple in design, yet the performance gains are through the roof. Thanks again.


One of the regulars around here (I always forget who) once called it the "Swiss Army Knife of SQL". They were absolutely correct. The usage of something so simple is truly amazing.


That would be none other than Gianluca Sartori, the "spaghetti dba".
http://spaghettidba.com/


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1426520
Posted Tuesday, March 05, 2013 1:40 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, February 20, 2014 10:38 AM
Points: 40, Visits: 148
Thanks Sean,
The name of the function required would have been 99% help, the brilliant article with an example was the icing on the cake !!!! totally worked for me, thanks again.
Post #1426641
Posted Tuesday, March 05, 2013 7:21 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 11,927, Visits: 10,967
Jeff Moden (3/4/2013)
Sean Lange (3/4/2013)
Ed Wagner (3/4/2013)
Thanks, Sean. Good article. You know, I'm consistently impressed by just how many different uses there are for the tally table. So simple in design, yet the performance gains are through the roof. Thanks again.


One of the regulars around here (I always forget who) once called it the "Swiss Army Knife of SQL". They were absolutely correct. The usage of something so simple is truly amazing.


That would be none other than Gianluca Sartori, the "spaghetti dba".
http://spaghettidba.com/


Thanks for remembering Jeff. His assessment is so spot on that I often refer to it myself the same.


_______________________________________________________________

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 Moden's 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)
Post #1426818
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse