Solved

Oracle SQL statement needed to align column values across the record

Posted on 2007-03-29
7
1,382 Views
Last Modified: 2013-12-19
I have a resultset like the following:

Name                  Type               Hours
---------------       ----------          -------------
John                   Direct              10
Jane                   Indirect            20
Mary                  Direct               15

That I need to look like this:

Name           Direct             Indirect
-------------   -------------    ------------
John            10                   0
Jane            0                     20
Mary            15                   0

I think this is possible can somebody provide the Oracle SQL?

Thanks.
0
Comment
Question by:dkbailey1
[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
7 Comments
 
LVL 4

Accepted Solution

by:
mukhtar2t earned 250 total points
ID: 18819011
select name,decode(type,'direct',hours,) Direct,,decode(type,'indirect',hours,) InDirect,
0
 
LVL 2

Assisted Solution

by:zilva
zilva earned 150 total points
ID: 18819646
select
  name,
  decode(Type,'Direct',Hours,'Indirect',0) Direct,
  decode(Type,'Indirect',Hours,'Direct',0) Indirect
from
  your_table
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18819988
Yes that it is i forgot the 0
select
  name,
  decode(Type,'Direct',Hours,'Indirect',0) Direct,
  decode(Type,'Indirect',Hours,'Direct',0) Indirect
from
 thetable
0
Technology Partners: 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 3

Expert Comment

by:harry_hendrata
ID: 18822347
try this, (x is thetable)

select tab1.name, NVL(tab2.hours,0) DIRECT,
       NVL(tab3.hours,0) INDIRECT
from   x tab1,
       x tab2,
       x tab3
where  tab1.name = tab2.name (+)  
and    tab1.name = tab3.name (+)
and    tab2.type (+) = 'DIRECT'
and    tab3.type (+) = 'INDIRECT'
0
 
LVL 8

Assisted Solution

by:YANN0S
YANN0S earned 100 total points
ID: 18823204
All the solutions are correct if Name is unique in that table. A more generic approach is the following:

SELECT NAME,
       SUM(DECODE (TYPE, 'Direct', hours, 0)) direct,
       SUM(DECODE (TYPE, 'Indirect', hours, 0)) indirect
  FROM your_table
 GROUP BY NAME

Pls note that I have changed the DECODE expression to DECODE (TYPE, 'Direct', hours, 0) instead of decode(Type,'Direct',Hours,'Indirect',0) (i.e. defaulting to 0 when type is different than 'Direct'). This can be usefull if you have more than one distinct values in TYPE column.

SELECT NAME,
       SUM(DECODE (TYPE, 'Direct', hours, 0)) direct,
       SUM(DECODE (TYPE, 'Indirect', hours, 0)) indirect,
       SUM(DECODE (TYPE, 'Other', hours, 0)) other
  FROM your_table
 GROUP BY NAME

Unfortunatelly you have to know the distinct values of the TYPE column before writing the statement (since you have to know, therefore there is no single statement that would work for 2,3,4,...etc distinct values of TYPE).
0
 
LVL 8

Expert Comment

by:YANN0S
ID: 18823214
Sorry I forgot: I would not prefer the self outer join solution, Why adding overhead with joins etc?
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18823235
Yes while there is a solution without join that is better
0

Featured Post

Technology Partners: 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

Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

734 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