Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to split query result into seperate columns


How to split query result into seperate columns

Author
Message
brett.y
brett.y
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 166
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
Sean Lange
Sean Lange
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: 16576 Visits: 17020
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)
Ed Wagner
Ed Wagner
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10285 Visits: 9581
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
String Splitting with True Performance
Best practices on how to ask questions
Sean Lange
Sean Lange
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: 16576 Visits: 17020
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. Cool

_______________________________________________________________

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)
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45119 Visits: 39918
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. Cool


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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
brett.y
brett.y
SSC Rookie
SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)SSC Rookie (42 reputation)

Group: General Forum Members
Points: 42 Visits: 166
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. Wow
Sean Lange
Sean Lange
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: 16576 Visits: 17020
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. Cool


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. :-P

_______________________________________________________________

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)
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