Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Oracle SQL Transpose Multiple Record

Posted on 2006-06-13
4
Medium Priority
?
1,265 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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ā€¦
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

618 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