By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,659 Members | 1,678 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,659 IT Pros & Developers. It's quick & easy.

Transpose Rows to Columns ?

P: n/a
I have records

ID Sku Name Date
2 41 Blair 01/04/03
3 45 John 03/04/03

that should look like...

ID 2 3
Sku 41 45
Name Blair John
.....

and so on. Number of source rows will be fixed (12) so no of target
columns will be 12 too.

Anyone know of a quick way to do this via simple SQL ?

Mar 29 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
bo****@gmail.com wrote:
I have records

ID Sku Name Date
2 41 Blair 01/04/03
3 45 John 03/04/03

that should look like...

ID 2 3
Sku 41 45
Name Blair John
....

and so on. Number of source rows will be fixed (12) so no of target
columns will be 12 too.

Anyone know of a quick way to do this via simple SQL ?

This should work on all RDBMS:
CREATE TABLE Sales(Year INTEGER,
Quarter INTEGER,
Results INTEGER);
SELECT Year,
MAX(CASE WHEN Quarter = 1
THEN Results END) AS Q1,
MAX(CASE WHEN Quarter = 2
THEN Results END) AS Q2,
MAX(CASE WHEN Quarter = 3
THEN Results END) AS Q3,
MAX(CASE WHEN Quarter = 4
THEN Results END) AS Q4
FROM Sales
GROUP BY Year

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 30 '06 #2

P: n/a
I understand SQL 2005 T-SQL has a Pivot command. Would that do it?

Mar 30 '06 #3

P: n/a
ji**********@countrywide.com wrote:
I understand SQL 2005 T-SQL has a Pivot command. Would that do it?

OP didn't state the version....

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Mar 30 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.