Solved

Oracle SQL Transpose Multiple Record

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

Join & Write a Comment

Truncate is a DDL Command where as Delete is a DML Command. Both will delete data from table, but what is the difference between these below statements truncate table <table_name> ?? delete from <table_name> ?? The first command cannot be …
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
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…

707 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

18 Experts available now in Live!

Get 1:1 Help Now