Solved

Need help with formulating a query

Posted on 2011-02-18
1
219 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
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

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

Suggested Solutions

Title # Comments Views Activity
Oracle Pivot Question 8 71
exp/imp 25 87
grant user/role question 11 32
Error querying database link from Oracle 10g to Postgresql 7 68
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…
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 …
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
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.

830 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