Solved

Oracle SQL Transpose Multiple Record

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

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
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 video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

867 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

21 Experts available now in Live!

Get 1:1 Help Now