Solved

get oracle rows as columns

Posted on 2009-07-15
7
708 Views
Last Modified: 2012-05-07
I need to write a report from oracle sql that outputs the rows like columns so the analyst can compare the value
The output from oracle comes out to be
Year month   Product   Stats
200901         ABC          50.00
200902         ABC           40.00

it should look like
Product  Jan2009   STATS    Feb 2009  STATS  DElta (col3-5)
0
Comment
Question by:mahjag
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24865540
Are you looking for an arbitrary number of columns or just the two columns with the delta?
0
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 125 total points
ID: 24865555
Assuming table name is samp

select yearmonth1, stats1, yearmonth2, stats2, stats1 - stats2 as delta from
 (select yearmonth as yearmonth1, stats as stats1 from samp where yearmonth = to_date('200901', 'yyyymm')) a,
 (select yearmonth as yearmonth2, stats as stats2 from samp where yearmonth = to_date('200902', 'yyyymm')) b
;

Open in new window

0
 

Author Comment

by:mahjag
ID: 24866164
I changed table name and executed it and I got dup rows of about 300 - am I missing any join here
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24866312
The sample data you gave contained only two rows. Perhaps you can explain your data in more detail?
0
 
LVL 41

Expert Comment

by:Sharath
ID: 24866844
How many rows and columns you have in your table?
0
 

Author Comment

by:mahjag
ID: 24869540
I got the sql t work it was missing the where clause which I have to restrict,my only question is the table that I am refering to is already a view of 2 bigger tables of more than million rows, the query takes more time to execute just for a month as against directly querying the table for a month -
0
 
LVL 40

Expert Comment

by:mrjoltcola
ID: 24869719
Yes, that is because views do not help speed up anything. they are simply stored queries. If you want to speed it up you'll need to create indexes on the columns that you are searching / limiting by.

Possibly the month column is not indexed.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article started out as an Experts-Exchange question, which then grew into a quick tip to go along with an IOUG presentation for the Collaborate confernce and then later grew again into a full blown article with expanded functionality and legacy…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question