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
Solved

WHERE = (value in from a subselect).......         (This SQL works, just want your opinion on it ) Part 2

Posted on 2010-11-30
5
519 Views
Last Modified: 2012-08-30
Hi EE,

In a related question, and EE expert taught me how to improve my sql when testing a WHERE to a subselect.

I am in a similar situation,however:
    in the prev post, i wanted the max for a student's id

It's possible for a student to complete multiple degrees, for ex:
    MED         MD  DEGREE   05-31-13
    PH           PHD DEGREE   12-31-12

Both degrees can have diff. graduation dates. The vendor's pgm allows the user to go in an even generate multiple
degree rows (don't know why that would happen) and assigns a seq # like this:

    MED             MD  DEGREE   05-31-13     1
    PH               PHD DEGREE   12-31-12     1
    PH               PHD DEGREE   12-31-12     2

so i want the graduation date for the max seq no like this

    MED             MD  DEGREE   05-31-13     1
     PH               PHD DEGREE   12-31-12     2

So i figured, would have to group by student_id, College, Degree

and then i correlated like this:
                            WHERE b.person_uid = a.person_uid
                         and
                b.college = a.college
                         and
                b.program = a.program        


I'd like to double check with you that coding the above WHERE in the sub select was the right approach.
I think so, bec. got desired results...

Below are screen shots:
    a) detail rows for a student (student id is not shown as one of the columns)
    b) results of the sql that appears in the code window below...


tx for your help, sandra

                   

SELECT   a.person_uid
        ,a.college
        ,a.program
        ,a.curriculum_order
        ,a.outcome_graduation_date
FROM academic_outcome a
WHERE a.person_uid = 18429
            AND     
      a.curriculum_order =   
         (
          select max(curriculum_order)
          from academic_outcome b
          WHERE b.person_uid = a.person_uid
                         and
                b.college = a.college
                         and
                b.program = a.program         
          group by person_uid, college, program
         )

Open in new window

2010-11-30a-adg-dtl.GIF
2010-11-30b-adg-dtl.GIF
0
Comment
Question by:mytfein
5 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 34241095
yes, that is just fine, IMHO
with the indexes you seem to have, a nice explain plan should also ensure good execution
0
 

Author Comment

by:mytfein
ID: 34241120
Thx Angel,

tx for writing and your validation, s
0
 
LVL 14

Expert Comment

by:leoahmad
ID: 34241122
does not seems any problem
0
 
LVL 20

Expert Comment

by:gatorvip
ID: 34241144
   PH               PHD DEGREE   12-31-12     1
    PH               PHD DEGREE   12-31-12     2

How are these two ordered - what decides whether it's a "1" or a "2"?

You could accomplish what you want with analytics. Something like the code below should work:


SELECT   person_uid
        ,college
        ,program
        ,curriculum_order
        ,outcome_graduation_date
from        
(
	SELECT   a.person_uid
		,a.college
		,a.program
		,a.curriculum_order
		,a.outcome_graduation_date
		,row_number() over(partition by person_uid, college, program order by curriculum_order desc) rn
	FROM academic_outcome a
	WHERE a.person_uid = 18429
)
where rn=1

Open in new window

0
 

Author Comment

by:mytfein
ID: 34241271
Hi Leo and Gator,

tx for writing,  sorry that i closed the question as done while you were replying....

Hi Gator,
    i understand your suggestion as another approach.... i could sort by approp column so that
    the correct row gets tagged with row#1 for rn=1

tx again everyone, s

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
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 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 videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

790 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