Solved

Oracle SQL statement needed to align column values across the record

Posted on 2007-03-29
7
1,375 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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.

Join & Write a Comment

This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
Creating and Managing Databases with phpMyAdmin in cPanel.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now