473,409 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

Turning Rows into Columns

Say I have a table of data containing something like

Region | County | Year | Month | Value

for some sort of value (int). I want to re-arrange this data so that it
comes out like this:

Region | County | Year | J | F | M | A | M | J | J | A | S | O | N | D

where the letters are obviously the months in order. How would I go
about this/what's the best way. I attempted to use 12 INNER JOINS on
the table itself, sadly that failed miserably. Also, this doesn't seem
very efficient?

Before you ask I got rid of my original code (gave up!)

Jan 23 '07 #1
4 7376
What you need is table pivoting (normally done best in reporting tools). If
you have to do it in SQL, here are a couple options:

SQL Server 2005 only (using the PIVOT table operator, you can look it up in
SQL Server 2005 BOL):

SELECT M1.Region, M1.County, M1.Year,
SUM([Jan]) AS [Jan],
SUM([Feb]) AS [Feb],
SUM([Mar]) AS [Mar],
SUM([Apr]) AS [Apr],
SUM([May]) AS [May],
SUM([Jun]) AS [Jun],
SUM([Jul]) AS [Jul],
SUM([Aug]) AS [Aug],
SUM([Sep]) AS [Sep],
SUM([Oct]) AS [Oct],
SUM([Nov]) AS [Nov],
SUM([Dec]) AS [Dec]
FROM MyTable AS M1
INNER JOIN (SELECT Region, County, Year, Value, Month
FROM MyTable) AS M2
PIVOT ( SUM(Value) FOR Month IN ([Jan],
[Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov],
[Dec])) AS M3
ON M1.Region = M3.Region AND M1.County = M3.County AND
M1.Year = M3.Year
GROUP BY M1.Region, M1.County, M1.Year
SQL Server 2000 and SQL Server 2005:

SELECT Region, County, Year,
SUM(case when Month = 'Jan' then Value else 0 end) AS [Jan],
SUM(case when Month = 'Feb' then Value else 0 end) AS [Feb],
SUM(case when Month = 'Mar' then Value else 0 end) AS [Mar],
SUM(case when Month = 'Apr' then Value else 0 end) AS [Apr],
SUM(case when Month = 'May' then Value else 0 end) AS [May],
SUM(case when Month = 'Jun' then Value else 0 end) AS [Jun],
SUM(case when Month = 'Jul' then Value else 0 end) AS [Jul],
SUM(case when Month = 'Aug' then Value else 0 end) AS [Aug],
SUM(case when Month = 'Sep' then Value else 0 end) AS [Sep],
SUM(case when Month = 'Oct' then Value else 0 end) AS [Oct],
SUM(case when Month = 'Nov' then Value else 0 end) AS [Nov],
SUM(case when Month = 'Dec' then Value else 0 end) AS [Dec]
FROM MyTable
GROUP BY Region, County, Year
In both cases you have to replace the Jan, Feb, etc. with the correct
spelling of the months in your database.

Regards,

Plamen Ratchev
http://www.SQLStudio.com
Jan 23 '07 #2
Oops, no need to SUM when using PIVOT, it should be like this:

SELECT DISTINCT M1.Region, M1.County, M1.Year,
[Jan],
[Feb],
[Mar],
[Apr],
[May],
[Jun],
[Jul],
[Aug],
[Sep],
[Oct],
[Nov],
[Dec]
FROM MyTable AS M1
INNER JOIN (SELECT Region, County, Year, Value, Month
FROM MyTable) AS M2
PIVOT ( SUM(Value) FOR Month IN ([Jan],
[Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov],
[Dec])) AS M3
ON M1.Region = M3.Region AND M1.County = M3.County AND
M1.Year = M3.Year
ORDER BY M1.Region, M1.County, M1.Year

Regards,

Plamen Ratchev
http://www.SQLStudio.com

Jan 23 '07 #3
Thanks Plamen, much appreciated. I'm using SQL Server 2000 for this
project but for future knowledge which is the best to use (in this
case) speed/efficiency wise?

Thanks again
Plamen Ratchev wrote:
Oops, no need to SUM when using PIVOT, it should be like this:

SELECT DISTINCT M1.Region, M1.County, M1.Year,
[Jan],
[Feb],
[Mar],
[Apr],
[May],
[Jun],
[Jul],
[Aug],
[Sep],
[Oct],
[Nov],
[Dec]
FROM MyTable AS M1
INNER JOIN (SELECT Region, County, Year, Value, Month
FROM MyTable) AS M2
PIVOT ( SUM(Value) FOR Month IN ([Jan],
[Feb], [Mar], [Apr], [May], [Jun], [Jul], [Aug], [Sep], [Oct], [Nov],
[Dec])) AS M3
ON M1.Region = M3.Region AND M1.County = M3.County AND
M1.Year = M3.Year
ORDER BY M1.Region, M1.County, M1.Year

Regards,

Plamen Ratchev
http://www.SQLStudio.com
Jan 23 '07 #4
I have not done deep analysis on performance but I would guess there will
not be much difference. It seems the PIVOT operator in SQL Server 2005
applies similar logic to the one used with the CASE technique. Using the
CASE technique is very efficient because it scans the base table only once.
You can always compare execution plans for the particular case and see which
one performs better.

The PIVOT operator is a bit confusing at first look and takes time to get
used to the syntax. One benefit is that it allows writing shorter code.

Regards,

Plamen Ratchev
http://www.SQLStudio.com
Jan 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Diego TERCERO | last post by:
Hi... I'm working on a tool for editing text resources for a family of software product my company produces. These text resources are found in a SQL Server database, in a table called...
0
by: Ireneus Broncel | last post by:
I have a class which reads Groups and Users from ActiveDirectory. The Problem is, that i have about 10000 rows as product. When I am trying to read the "memberOf" Objects out of this field i get...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
2
by: Erik Cruz | last post by:
Hi. I have a repeater control bound to an ArrayList. When displaying the data, I need to show 3 columns per row, each column being one entry of my ArrayList, but the repeater understands that...
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
1
by: Lars E | last post by:
Hi all I have a small problem. I have a datatable with 8 columns. But it is only data in 5 of the columns. Data for the remaing 3 columns is in another dataset. I Want to run trough the...
0
by: sysmanint1 | last post by:
I am a total neophyte at Visual Basic but found the following post and reply from Clint concerning a dynamic range. Also, have never "posted" to a discussion I have made a macro that works on...
3
by: hardcode | last post by:
I can't do it in Crystal Repots either without making a formula for each field so I thought about using 'copy an existing table structure wo/the data' (like to make a new table and append records to...
7
by: lethek39 | last post by:
Hey I have been trying to figure out how to sum rows and columns in a matrix square. I also have been trying to get the program to list the numbers of the diagonal in the matrix. So far this is the...
1
by: tucson | last post by:
I have a gridview that has a blank row with 2 input fields: file to upload, and a description of the file. They click Add and a new row is added, Remove and the row is removed. The problem is:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.