Need help for SQL query

I have the following table

State     City      Zipcode
aa            a1        12345
aa            a1          NA

aa            b1        5889      
aa            b1         NA

bb            b1         2245
bb            b1         NA

bb           c1          7788
bb           c1          NA

dd            d1         15678

ee             d1          NA

From the above table I have duplicate records in zipcode (example state aa  and city a1 has a zipcode '12345' but it has another record 'NA' - I want to delete this NA record)

 I have to delete the ZIPCODE='NA'  if only state and city has the  appropriate zipcode.
Do not delete - If  state and city does not have appropriate zipcode and it only has NA


State     City      Zipcode
aa            a1        12345
aa            b1        5889      
bb            b1         2245
bb           c1          7788
dd            d1         15678
ee             d1          NA
Varshini SAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Please post concise and complete examples. Include table DDL and sample data INSERT statements. ASCII arts is - well - kinda annoying..


      [State] CHAR(2) ,
      City CHAR(2) ,
      Zipcode VARCHAR(5)

VALUES  ( 'aa', 'a1', '12345' ),
        ( 'aa', 'a1', 'NA' ),
        ( 'aa', 'b1', '5889' ),
        ( 'aa', 'b1', 'NA' ),
        ( 'bb', 'b1', '2245' ),
        ( 'bb', 'b1', 'NA' ),
        ( 'bb', 'c1', '7788' ),
        ( 'bb', 'c1', 'NA' ),
        ( 'dd', 'd1', '15678' ),
        ( 'ee', 'd1', 'NA' );

WITH    Ordered
          AS ( SELECT   * ,
                        ROW_NUMBER() OVER ( PARTITION BY [State], City ORDER BY CASE WHEN Zipcode = 'NA' THEN 1
                                                                                     ELSE 0
                                                                                END ) AS RN
               FROM     @Sample S
    SELECT  [State] ,
            City ,
    FROM    Ordered
    WHERE   RN = 1;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.