Home » SQL & PL/SQL » SQL & PL/SQL » using data as column header
using data as column header [message #20086] Mon, 29 April 2002 19:46 Go to next message
Andrew Rosenberg
Messages: 5
Registered: April 2002
Junior Member
I don't think this is a terribly complex problem, but i can't seem to figure it out. I originally posted this message on the newbie board but have gotten no responses since last week.

I have data that looks like this:
col1 col2 total
---- ---- -----
a A 1
a B 3
b A 2
b B 1

I'd like a report that displays this as

col1 A B
---- - -
a 1 3
b 2 1

any advice is welcome. My apologies for taking your time on an issue that may not be as complex as this board is meant for.

thanks in advance,
Andrew
Re: using data as column header [message #20096 is a reply to message #20086] Tue, 30 April 2002 01:43 Go to previous messageGo to next message
Shankar. V
Messages: 5
Registered: April 2002
Junior Member
Mr. Andrew,

I have created a table based on your table as below and then I added the sample data as you given:

create table demo (
col1 varchar2(3),
col2 varchar2(3),
total number);

Query:
******
select col1,
sum(decode(col2,'A',total)) A,
sum(decode(col2,'B',total)) B
from demo group by col1;

I hope this serves the purpose.
Re: using data as column header [message #20113 is a reply to message #20086] Tue, 30 April 2002 03:56 Go to previous messageGo to next message
Amit
Messages: 166
Registered: February 1999
Senior Member
--try this

select col1 , max(total) A, min(total) B
from table_name
group by col1
Re: using data as column header - respecification [message #20121 is a reply to message #20086] Tue, 30 April 2002 06:39 Go to previous message
Andrew Rosenberg
Messages: 5
Registered: April 2002
Junior Member
Thank both of you for your help, but my problem is slightly more complex, as i need the column names to be dynamically generated, based on the table data.

the data could look like
col1 col2 total
---- ---- -----
a A 1
b A 3
a B 3
b B 4

or

col1 col2 total
---- ---- -----
a B 1
a B 3
a D 3
a D 4

i need to display the previous as:
col1 A B
---- - -
a 1 3
b 3 4

and the latter as
col1 B D
---- - -
a 1 3
b 3 4

essentially i need to convert the data in col2 to the column header for the report.

thank you again
-andrew
Previous Topic: Re: Difference between oracle and SQL Server
Next Topic: pl/sql
Goto Forum:
  


Current Time: Sat May 04 14:48:02 CDT 2024