Solved

Need help with formulating a query

Posted on 2011-02-18
1
227 Views
Last Modified: 2012-05-11
I have two tables - address and code_map

address table contains - id, seq_nbr,addr_type, street, state, country,natn_cd
code_map contains natn_cd, nation_desc

The valid addr_types are PR,MA,LO. Whenever a new PR address type is added for an id, the seq_nbr gets incremenented

Ex.

id                 seq_nbr            addr_type         street    state      natn_cd
1234               1                      PR                     abc      MD         0001
1234               2                      PR                     xyz      MD         0001
1234               1                      MA                    qwe    MD          0001

And then code_map contains 0001       USA

I need to write a query which will fetch me an address with the following priority - first look for PR, then MA, then LO. And within each addr_type the row with the maximum seq_nbr should be returned. Also, the fields to be returned should include nation_desc so a join with code_map should be performed.

Thanks
0
Comment
Question by:happylife1234
[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
1 Comment
 
LVL 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 34927113
Something like (typed in, untested):

select nation_desc
from code_map c,
(select natn_cd, row_number() over(partition by id order by decode(addr_type,'PR',1,'MA',2,'LO',3,999) asc, seq_nbr desc) myrownum
) x
where x.myrownum=1 and x.natn_cd=c.natn_cd;
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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
This video shows how to recover a database from a user managed backup
Suggested Courses

623 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