Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
  #1  
Old July 26th, 2004, 02:10 PM
OracleGreenHorn OracleGreenHorn is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 4 OracleGreenHorn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 36 sec
Reputation Power: 0
Arrow Grouping in Oracle

Hi Guys,

I am getting back to Oracle after a long gap of 4 yrs. I have a basic SQL question. Here it is.

I have a query that joins multiple tables and the result is as follows

Name MyCol1 MyCol2 MyCol3
---------------------------------------
ABC 1 - -
ABC - 2 -
ABC - - 3
PQR 2 - -
PQR - 5 -
PQR - - 4
XYZ 6 - -
XYZ - 1 -
XYZ - - 2


The table structures are as follows

Table1 (Parent table)
------
Name

Table2 (Child table)
------
Name (References Name from Table1)
ColName (Which could be MyCol1/MyCol2/MyCol3)
ColValue (A corresponding value for ColName)

This is how my query is ...

SELECT t1.Name,
DECODE (t2.ColName, 'MyCol1', t2.ColValue) MyCol1,
DECODE (t2.ColName, 'MyCol2', t2.ColValue) MyCol2,
DECODE (t2.ColName, 'MyCol3', t2.ColValue) MyCol3
FROM Table1 t1, Table2 t2
WHERE t1.Name = t2.Name

What would be easiest way to get my result look like follows...

Name MyCol1 MyCol2 MyCol3
---------------------------------------
ABC 1 2 3
PQR 2 5 4
XYZ 6 1 2

Is it possible to achieve this using a query and not writing a PL/SQL script?

Please advice.

Regards,
OracleGreenHorn

Reply With Quote
  #2  
Old July 26th, 2004, 06:21 PM
gamyers gamyers is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 14 gamyers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The simplest is :
SELECT t1.Name,
MAX(DECODE (t2.ColName, 'MyCol1', t2.ColValue)) MyCol1,
MAX(DECODE (t2.ColName, 'MyCol2', t2.ColValue)) MyCol2,
MAX(DECODE (t2.ColName, 'MyCol3', t2.ColValue)) MyCol3
FROM Table1 t1, Table2 t2
WHERE t1.Name = t2.Name
group by t1.Name

The warning is that, it assumes you will not have duplicates (ie the following situation would never occur). If it could, you'll have to decide what you want your SQL to do in those circumstances (eg sum up the values, show the MAX or MIN, raise an exception....)

ABC 1 - -
ABC - 2 -
ABC - 3 -
ABC - - 4

Reply With Quote
  #3  
Old July 26th, 2004, 07:04 PM
OracleGreenHorn OracleGreenHorn is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 4 OracleGreenHorn User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 26 m 36 sec
Reputation Power: 0
Thanks !

Gamyers,

I appreciate your help. It worked for me and it's good enough for the time being.

Thanks again,
OracleGreenHorn

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Grouping in Oracle


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway