Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > Oracle
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
Oracle General Oracle database discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Oracle section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 17th, 2004, 02:19 AM
Authorized User
 
Join Date: Jun 2003
Location: Dhahran, Eastern Province, Saudi Arabia.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL Query to Convert Columns into Rows

I have a Table which looks like

name, year, value
---------------------
john, 1991, 1000
john, 1992, 2000
john, 1993, 3000
jack, 1991, 1500
jack, 1992, 1200
jack, 1993, 1340
mary, 1991, 1250
mary, 1992, 2323
mary, 1993, 8700
and so on

I want to perform a sql query to return results like this:
year, john, Jack, mary ...
1991, 1000, 1500 1250
1992, 2000, 1200, 2323
1993, 3000, 1340, 8700

Any hint will be greatly appreciated.
  #2 (permalink)  
Old April 17th, 2004, 01:30 PM
Authorized User
 
Join Date: Jul 2003
Location: , , .
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Niaz,

You can accomplish this by a "pivot" query. Please look at the small testcase that I prepared below:

SQL> desc t1
 Name Null? Type
 --------------- -------- ----------------
 NAME VARCHAR2(10)
 YEAR NUMBER(4)
 VALUE NUMBER(4)

SQL>
SQL> select * from t1;

NAME YEAR VALUE
---------- ---------- ----------
john 1991 1000
john 1992 2000
john 1993 3000
jack 1991 1500
jack 1992 1200
jack 1993 1340
mary 1991 1250
mary 1992 2323
mary 1993 8700

9 rows selected.

SQL> -- now, try out the pivot query
SQL> select year,
  2 max( decode( name, 'john', value, null ) ) "JOHN",
  3 max( decode( name, 'jack', value, null ) ) "JACK",
  4 max( decode( name, 'mary', value, null ) ) "MARY"
  5 from
  6 (
  7 select name, year, value
  8 from t1
  9 )
 10 group by year ;

      YEAR JOHN JACK MARY
---------- ---------- ---------- ----------
      1991 1000 1500 1250
      1992 2000 1200 2323
      1993 3000 1340 8700

SQL>

Hope that helps.

Cheers,
Prat


  #3 (permalink)  
Old April 18th, 2004, 12:04 AM
Authorized User
 
Join Date: Jun 2003
Location: Dhahran, Eastern Province, Saudi Arabia.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Prat.

I also created a similar Query using DECODE and GROUP BY.

Regards

Niaz
  #4 (permalink)  
Old February 5th, 2010, 07:08 AM
Registered User
 
Join Date: Feb 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Hey

Hi,

I have a similar requirement, the only constraint being the fact that I have Strings instead of integers. So, the max() will not work.

Is there a way around that ?

Thanks
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Query to convert Columns into Rows....again phix SQL Server 2000 11 November 11th, 2007 09:59 AM
convert columns to rows in sql hoangmv0101 SQL Language 0 January 2nd, 2007 02:04 AM
Formatting sql query rows as columns with stack sastian PHP Databases 0 March 25th, 2005 04:51 AM
getting a query rows as columns raamts SQL Language 1 February 24th, 2005 04:06 AM
SQL Query to Convert Columns into Rows Niaz SQL Server 2000 2 April 20th, 2004 01:36 AM



All times are GMT -4. The time now is 01:24 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.