Alfredo
Alfredo

Reputation: 3

Select rows where ID is repeated, but value in another column is different

I have a table like this:

FRUIT TABLE

What I would like to be able to do is return the IDs and how many different types of fruit the ID is associated with other than 1 like so:

OUTPUT

Can someone help me out? I don't think it should be that difficult, but I haven't had much luck.

Thanks!

Upvotes: 0

Views: 216

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1269493

I think you want the having clause and count(distinct):

select id, count(distinct fruit) as numfruit
from t
group by id
having count(distinct fruit) > 1;

Upvotes: 1

Ilyes
Ilyes

Reputation: 14928

Here you go:

select id, count (id)
from yourtable
group by id;

Demo

Upvotes: 0

Related Questions