Solved

Sql Query in oracle sql

Posted on 2011-09-29
15
366 Views
Last Modified: 2013-11-11

I have a table with two fields Country and Zip. say like
country-zip
US      60074
US      99001-9900
us      99000
IN      600741
IN      834938
CA      1A2 C3G
CA      9K0 I9Y
PK      999
GB      9999999


I want the result to be something like this :
country-zip
US      99001-9900
us      99000
IN      600741
CA      1A2 C3G
PK      999
GB      9999999

It can choose any zip for one country if there are two.
Please help

I need to retrive the data based on the zipcode format like if for 'US'
there the 2 different formats like 60074 and 60074-9430 In this case i need to retrive both the records.

Database Version : Oracle 11.X
0
Comment
Question by:himabindu_nvn
  • 5
  • 3
  • 3
  • +2
15 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36816046
I don't understand the piece about BOTH records if the formats exist.  Do you mean if the first 5 are the same and one is zip+4 retuirn both but if the fisrt 5 don't match, pick one?
0
 
LVL 15

Expert Comment

by:Devinder Singh Virdi
ID: 36816050
use select * from (select county, zip, row_number() over(partition by county order by zip) rank from countyzip) where rank <= 2
0
 

Author Comment

by:himabindu_nvn
ID: 36816129
There are multiple zipcode formats for one country then i need to retrive atleast one record for each zipcode format.
Like.
US      99999
US      99999-9999
US      93838-8943
US      83293
CA      9G8 A8T
CA      8F9 A9A
CA      W1F 9J3
PL      190-9292
PL      919-9929
IN      922299
IN      923092

Output:
US      99999
US      93838-8943
CA      9G8 A8T
CA      W1F 9J3
PL      190-9292
IN      922299

Need to select atleast once record for each number format..
0
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.

 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36816135
virdi_ds,

I don't think that will meet this requirement:  "i need to retrive both the records"
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 36816152
I don't see how you pick:
CA      9G8 A8T
CA      W1F 9J3


from:
CA      9G8 A8T
CA      8F9 A9A
CA      W1F 9J3


What defines 'different formats' here?
0
 

Author Comment

by:himabindu_nvn
ID: 36816200
CA      9G8 A8T  - in this it has NUMBER|CHAR|NUMBER CHAR|NUMBER|CHAR
CA      W1F 9J3  - CHAR|NUMBER|CHAR NUMBER|CHAR|NUMBER
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 36816407
are those the only two formats?  are do you need to check for every possible permutation of character/number?
0
 
LVL 32

Expert Comment

by:awking00
ID: 36905060
I don't have database access at the moment, but try the attached.
query.txt
0
 
LVL 32

Expert Comment

by:awking00
ID: 36905073
I think it may require a slight modification. Try again.
query.txt
0
 
LVL 32

Expert Comment

by:awking00
ID: 36917143

Missing single quote. One more time.
query.txt
0
 

Author Comment

by:himabindu_nvn
ID: 36988418
I've requested that this question be deleted for the following reason:

found solution
0
 
LVL 32

Expert Comment

by:awking00
ID: 36988420
I think it's only fair that when a questioner requests a delete because he or she found a solution, that the solution should be posted before the delete is allowed.
0
 
LVL 32

Accepted Solution

by:
awking00 earned 125 total points
ID: 36993051
Attached is the proof that my offered solution works for the two sets of example data provided in this thread. If the questioner found a better method, I would like to see it.
validation.txt
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

Title # Comments Views Activity
Wrong number of values in the INTO list of a FETCH statement 16 89
How to free up undo space? 3 50
Oracle DBLINKS From 11g to 8i 3 49
oracle forms question 22 42
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

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