Solved

Need help with formulating a query

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

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

Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

813 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

10 Experts available now in Live!

Get 1:1 Help Now