?
Solved

Oracle SQL Transpose Multiple Record

Posted on 2006-06-13
4
Medium Priority
?
1,263 Views
Last Modified: 2010-05-18
Hoping this is relatively simple, but my SQL is not up to it.

We have a peice of SQL that returns up to 2 of the latest address records for students.

e.g. SELECT * FROM (SELECT stud_id, surname, forename, dob, address1, address2, address3,
      ROW_NUMBER() OVER (PARTITION  BY stud_id ORDER BY address.start_date DESC) row_num

       FROM student, addresses
      WHERE student.stud_id = addresses.entity_id(+))
      WHERE row_num <3

If a student has 2 or more addresses then 2 records are returned for that student.

I need to return a single record per student with both addresses shown (as seperate fields) within that record

e.g. stud_id, surname, forename, dob, address1, address2, address3, address1-2, address2-2, address3-2

Any ideas would be much appreciated (Oracle ver is 9.02 if that helps). Would prefer a pure SQL solution if possible.

Thanks

Andy
0
Comment
Question by:phatmankerr
[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
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
GGuzdziol earned 2000 total points
ID: 16892974
SELECT stud_id, MAX(surname), MAX(forename), MAX(dob),
    MAX(DECODE(row_num, 1, address1, NULL)),
    MAX(DECODE(row_num, 1, address2, NULL)),
    MAX(DECODE(row_num, 1, address3, NULL)),
    MAX(DECODE(row_num, 2, address1, NULL)),
    MAX(DECODE(row_num, 2, address2, NULL)),
    MAX(DECODE(row_num, 2, address3, NULL))
  FROM (
    SELECT stud_id, surname, forename, dob, address1, address2, address3,
        ROW_NUMBER() OVER (PARTITION  BY stud_id ORDER BY address.start_date DESC) row_num
      FROM student, addresses
      WHERE student.stud_id = addresses.entity_id(+)
  )
  WHERE row_num <3
  GROUP BY stud_id
0
 

Author Comment

by:phatmankerr
ID: 16897603
Thanks mate,

That appears to work, is there anyway to make it more efficient, it is returning upwards of 70,000 rows and runs fairly slowly.

Don't worry if not, will assign you the points anyway as it works fine.

Cheers

Andy
0
 
LVL 14

Expert Comment

by:GGuzdziol
ID: 16900685
You mean there is big overhead in part I added? i.e. how long does it take to run only inner query and how long all of this?
0
 

Author Comment

by:phatmankerr
ID: 16900837
Apologies - a before and after comparison indicates that your additional code has very little impact - we appear to have some server performance issues.

Thanks for your help.
0

Featured Post

Independent Software Vendors: 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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.
Suggested Courses

777 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