Log in
::
Register
::
Not logged in
Home
Tags
Articles
Editorials
Stairways
Forums
Scripts
Videos
Blogs
QotD
Books
Ask SSC
SQL Jobs
Training
Authors
About us
Contact us
Newsletters
Write for us
Recent Posts
Recent Posts
Popular Topics
Popular Topics
Home
Search
Members
Calendar
Who's On
Home
»
SQL Server 2008
»
SQL Server Newbies
»
How to split query result into seperate...
How to split query result into seperate columns
Rate Topic
Display Mode
Topic Options
Author
Message
brett.y
brett.y
Posted Monday, March 04, 2013 8:45 AM
Grasshopper
Group: General Forum Members
Last Login: Friday, May 10, 2013 8:04 AM
Points: 15,
Visits: 81
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
Sean Lange
Sean Lange
Posted Monday, March 04, 2013 9:01 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 8,641,
Visits: 8,273
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
Post #1426299
Ed Wagner
Ed Wagner
Posted Monday, March 04, 2013 10:14 AM
SSCommitted
Group: General Forum Members
Last Login: Yesterday @ 1:55 PM
Points: 1,884,
Visits: 562
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.
Post #1426353
Sean Lange
Sean Lange
Posted Monday, March 04, 2013 10:16 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 8,641,
Visits: 8,273
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
Post #1426354
Jeff Moden
Jeff Moden
Posted Monday, March 04, 2013 4:49 PM
SSC-Dedicated
Group: General Forum Members
Last Login: Yesterday @ 4:51 PM
Points: 32,923,
Visits: 26,811
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 "
R
ow-
B
y-
A
gonizing-
R
ow".
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."
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #1426520
brett.y
brett.y
Posted Tuesday, March 05, 2013 1:40 AM
Grasshopper
Group: General Forum Members
Last Login: Friday, May 10, 2013 8:04 AM
Points: 15,
Visits: 81
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
Sean Lange
Sean Lange
Posted Tuesday, March 05, 2013 7:21 AM
SSCrazy Eights
Group: General Forum Members
Last Login: Yesterday @ 1:17 PM
Points: 8,641,
Visits: 8,273
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
Post #1426818
« Prev Topic
|
Next Topic »
Permissions
You
cannot
post new topics.
You
cannot
post topic replies.
You
cannot
post new polls.
You
cannot
post replies to polls.
You
cannot
edit your own topics.
You
cannot
delete your own topics.
You
cannot
edit other topics.
You
cannot
delete other topics.
You
cannot
edit your own posts.
You
cannot
edit other posts.
You
cannot
delete your own posts.
You
cannot
delete other posts.
You
cannot
post events.
You
cannot
edit your own events.
You
cannot
edit other events.
You
cannot
delete your own events.
You
cannot
delete other events.
You
cannot
send private messages.
You
cannot
send emails.
You
may
read topics.
You
cannot
rate topics.
You
cannot
vote within polls.
You
cannot
upload attachments.
You
may
download attachments.
You
cannot
post HTML code.
You
cannot
edit HTML code.
You
cannot
post IFCode.
You
cannot
post JavaScript.
You
cannot
post EmotIcons.
You
cannot
post or upload images.
Copyright © 2002-2013 Simple Talk Publishing. All Rights Reserved.
Privacy Policy.
Terms of Use.
Report Abuse.