简体   繁体   中英

How to compare two rows with same column

I have data like below. I need to compare ATTRIBUTE1_TXT based on PART_KEY_ID. If both the date is same, i need that PART_KEY_ID.

PART_KEY_ID ATTRIBUTE1_TXT
504277          JUL 2019 (201)
504277          JUL 2019 (201)
5493605         JUL 2019 (201)
5493605         JUN 2018 (191)
4585233         JUL 2019 (201)
4585233         JUL 2019 (201)
6192893         JUL 2019 (201)
6192893         JUN 2018 (191)

so output should be.

PART_KEY_ID
504277
4585233
select PART_KEY_ID 
from your_table
group by PART_KEY_ID 
having count(distinct ATTRIBUTE1_TXT) = 1

This should do what you want:

select part_key_id
from t
group by part_key_id
having min(ATTRIBUTE1_TXT) = max(ATTRIBUTE1_TXT);

This should work

select PART_KEY_ID from(select ATTRIBUTE1_TXT ,max(PART_KEY_ID),count(*) from table  group by ATTRIBUTE1_TXT having count(*) >1)t;
   --this will work 
  --if you got three similar  ATTRIBUTE1_TXT for one PART_KEY_ID you can replace 
  ----having count(*)=2 in cte to having count(*)=3

   ;with cte as (
    select count(*) c,PART_KEY_ID,ATTRIBUTE1_TXT
    from table
    group by PART_KEY_ID,ATTRIBUTE1_TXT
    having count(*)=2
    ) 

    select PART_KEY_ID
    from cte

Try this query, I think it should satisfy your need.

SELECT PART_KEY_ID
FROM table_name
GROUP BY PART_KEY_ID, ATTRIBUTE1_TXT
HAVING COUNT(*) > 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM