Solved

Oracle SQL Transpose Multiple Record

Posted on 2006-06-13
4
1,262 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 500 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that useā€¦
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

688 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