compare column value for each row

Dear experts
We have a table KEY_ID_LOCATION which has KEY,ID,LOCATION columns.

KEY            ID      LOCATION
cXZ            1      LN
cXZ            2      LN
cXZ            3      LN
cIR            1      NY
cIR            2      NY
cIR            3      NY
d5M        1      NY
d5M        2      NY
d5M        3      GP
d5M        4      MP
d5M        5      NY

In our application we first check if KEY already exists then just increment the ID and insert in the table
thats why for each KEY we have sequence of ID and the location.

I want to know if for each KEY there are two or more different LOCATION(s)

I am expecting the following result since for key d5M there are three different location:NY,GP,MP
d5M            1      NY
d5M            2      NY
d5M            3      GP
d5M            4      MP
d5M            5      NY

Any help with query will be appreciated.

select  KEY,ID,LOCATION	 from KEY_ID_LOCATION
where KEY IN  ('cXZ','cIR','d5M')  
and LOCATION = 
order by  KEY,ID,LOCATION with ur;

Open in new window


Thanks.
royjaydAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Wayne CarlsonCommented:
SELECT `KEY`,`ID`,`LOCATION` from `KEY_ID_LOCATION` where `KEY` IN
(SELECT `KEY` as `TARGETKEY` FROM
(SELECT `KEY`,COUNT(`KEY`) AS `KEYCOUNT` FROM
(SELECT DISTINCT `KEY`,`LOCATION`  FROM `KEY_ID_LOCATION` ) AS SQ1 GROUP BY `KEY`) AS SQ2 WHERE `KEYCOUNT`>1 )
Wayne CarlsonCommented:
Sorry, left out your ORDER BY:

SELECT `KEY`,`ID`,`LOCATION` from `KEY_ID_LOCATION` where `KEY` IN
(SELECT `KEY` as `TARGETKEY` FROM
(SELECT `KEY`,COUNT(`KEY`) AS `KEYCOUNT` FROM
(SELECT DISTINCT `KEY`,`LOCATION`  FROM `KEY_ID_LOCATION` ) AS SQ1 GROUP BY `KEY`) AS SQ2 WHERE `KEYCOUNT`>1 )
ORDER BY `KEY`,`ID`,`LOCATION`

Looks like you're using DB2.  I verified this with MYSQL.  There may be minor syntactical changes needed.
tliottaCommented:
There are a few variations that can work. This works for me:
with
  t1 as                                                
    (SELECT key1, location FROM KEY_ID_LOCATION GROUP BY key1, location),  
  t2 as                                                     
    (SELECT key1 FROM t1 GROUP BY key1 HAVING count(*)>1)     
SELECT KEY_ID_LOCATION.* FROM KEY_ID_LOCATION
 WHERE KEY_ID_LOCATION.key1 IN (SELECT key1 FROM t2)
 ORDER BY key1, id

Open in new window

I used KEY1 as a column name because KEY is a reserved word.

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
royjaydAuthor Commented:
thanks
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
DB2

From novice to tech pro — start learning today.