Solved

Oracle SQL statement needed to align column values across the record

Posted on 2007-03-29
7
1,381 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
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

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.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Via a live example, show how to take different types of Oracle backups using RMAN.
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.

830 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