Solved

Oracle SQL statement needed to align column values across the record

Posted on 2007-03-29
7
1,380 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
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…
This video shows how to recover a database from a user managed backup
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

809 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