简体   繁体   中英

group and have more than one value in sql

How do i find how many ids having type ids more than one..

Id Name Type_id
1  A    11
1  B    11
1  A    22
1  C    22
2  A    11
2  B    11
3  A    22
3  C    22
4  A    11
4  B    22

I need an out put which of the ids have more than one type id, in above example, Id 1 and 4 has more than one type ids 11,22

Output

Id  
1   
4   

This is pretty simple:

select id
from t
group by id
having min(type_id) <> max(type_id);

This is pretty much a direct translation of your description. (Note: You can use count(distinct) as well but that incurs more overhead.)

SELECT Id
FROM name_table
GROUP BY Id
HAVING COUNT(DISTINCT Type_id) > 1

In this query your are grouping all the record which have the same id, but just print the ones that have more than one distinct type_id

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