Skip to content
Advertisement

How can I look for unique values while having duplicate ids?

I have a table that has ids that repeat. I want an id that has all three values (TWIX, HERSHYS, and M&MS). The values can be on different lines but I do not know how to find the id by using statements that go line by line rather then by groups.

I have tried (in Microsoft SQL server)

This does not work due to SQL going line by line. How should I go about either creating flags or querying this in order to find which id group has all three values?

In this table I would want to find L123 but not F143.

Advertisement

Answer

You can use group by and having. If you want the ids that have all three brands:

cross apply is a convenient way of unpivoting the data, so you can deal with one column instead of two.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement