|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, December 07, 2012 4:08 AM
Points: 37,
Visits: 77
|
|
Hi Guys,
I am a bit stuck trying to pivot some data in SQL and was wondering if you could offer some guidance?!
My data looks like this:
create table TestData(Description varchar(255), Region_Code varchar(10), WholeACNet float)
insert into TestData values ('Original', 'AU', 1973026688) insert into TestData values ('RiverLakes_WWFL', 'AU', 112926275) insert into TestData values ('RiverLakesSev123_WWFL', 'AU', 1718559478) insert into TestData values ('Original', 'BR', 1000539676) insert into TestData values ('RiverLakes_WWFL', 'BR', 445413235) insert into TestData values ('RiverLakesSev123_WWFL', 'BR', 1059705496) insert into TestData values ('Original', 'CA', 2269091781) insert into TestData values ('RiverLakes_WWFL', 'CA', 890979921) insert into TestData values ('RiverLakesSev123_WWFL', 'CA', 997114545)
I would like to pivot it so the headers read: Region_Code, Original, RiverLakes_WWFL and RiverLakesSev123_WWFL
Any help would be much appreciated! 
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, November 08, 2012 9:15 AM
Points: 54,
Visits: 82
|
|
Would this work for you ? SELECT Original = CASE WHEN description = 'Original' THEN Region_Code ELSE '' END, RiverLakes_WWFL = CASE WHEN description = 'RiverLakes_WWFL' then Region_Code ELSE '' END, RiverLakesSev123_WWFL = CASE WHEN description = 'RiverLakesSev123_WWFL' THEN Region_Code ELSE '' END, WholeACNet FROM [TestAndLearn].[dbo].[TestData]
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, November 08, 2012 9:15 AM
Points: 54,
Visits: 82
|
|
I am new here. I need your advise. In my previous reply I designed queryin my SQL Server and copy paste here. It is looking flat text in my reply. I saw your post is in window with text color and has better readability. Could you tell me how did you post such a way that is much better than mine. Thank you
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, December 07, 2012 4:08 AM
Points: 37,
Visits: 77
|
|
Thanks for the reply....
Hmmm not quite what I was after.
This places the Region Code under the Original, RiverLaKes_WWFL and RiverLakesSev123_WWFL and gives the WholeACNet in one column.
Ideally Region_Code would be one column and the values for the 3 types to the right of that......
Thanks again!
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Friday, December 07, 2012 4:08 AM
Points: 37,
Visits: 77
|
|
T_Dot_Geek (10/31/2012) I am new here. I need your advise. In my previous reply I designed queryin my SQL Server and copy paste here. It is looking flat text in my reply. I saw your post is in window with text color and has better readability. Could you tell me how did you post such a way that is much better than mine. Thank you
When you compose a message on the left hand side there are some tags. Just wrap those around the code
[ c o d e = " o t h e r " ] *code goes here* [ / c o d e ] without the spaces!
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, November 08, 2012 9:15 AM
Points: 54,
Visits: 82
|
|
| Thanks. Could you please create a sample in excel including your columns values and position and post here for better understanding.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, November 08, 2012 9:15 AM
Points: 54,
Visits: 82
|
|
SELECT Region_Code, Original = COUNT(CASE WHEN description = 'Original' THEN 1 ELSE 0 END), RiverLakes_WWFL = COUNT(CASE WHEN description = 'RiverLakes_WWFL' then 1 ELSE 0 END), RiverLakesSev123_WWFL = count(CASE WHEN description = 'RiverLakesSev123_WWFL' THEN 1 ELSE 0 END) FROM [TestAndLearn].[dbo].[TestData] group by region_code
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Today @ 2:13 PM
Points: 186,
Visits: 1,572
|
|
| If your goal is to create a crosstab result, then by far the easiest way to do it is with a matrix report in Reporting Services. Did you need to have it returned as a SQL query result?
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Today @ 12:55 PM
Points: 294,
Visits: 1,102
|
|
Hi
Does this do what you want? I've used sum to aggregate the WholeACNet.
SELECT Region_Code, [Original], [RiverLakes_WWFL], [RiverLakesSev123_WWFL] FROM TestData PIVOT ( SUM(WholeACNet) FOR Description in ( [Original], [RiverLakes_WWFL], [RiverLakesSev123_WWFL] ) ) p
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Thursday, November 08, 2012 9:15 AM
Points: 54,
Visits: 82
|
|
Sorry for the previous post.Added count was not right. Here is a solution using CASE Statement.
SELECT Region_Code, Original = Max(CASE WHEN description = 'Original' THEN WholeACNet END), RiverLakes_WWFL = max(CASE WHEN description = 'RiverLakes_WWFL' then WholeACNet END), RiverLakesSev123_WWFL = Max(CASE WHEN description = 'RiverLakesSev123_WWFL' THEN WholeACNet END) FROM [TestAndLearn].[dbo].[TestData] group by region_code
|
|
|
|