loulou
loulou

Reputation: 1

Sql: Merge rows with same id and multiple attributes

I have a SQL table that looks like this:

Name | Attributes
-----------------
Toto | Attr1
Toto | Attr2
Titi | Attr1

and I would like a SQL request to merge the rows with attributes "Attr1" AND "Attr2" to have this table:

Name
----
Toto 

How can I do this? Thank you.

Upvotes: 0

Views: 328

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269563

If you want names that have both attributes, you can use group by:

select name
from t
where attributes in ('Attr1', 'Att2')
group by name
having count(distinct attributes) = 2;

Upvotes: 2

Related Questions