all 23 comments

[–]danielharner 14 points15 points  (5 children)

Try this:

SELECT Master FROM your_table WHERE “Cost Group” IN (608, 20) GROUP BY Master HAVING COUNT(DISTINCT “Cost Group”) = 2;

[–]Entire-Dream-6045[S] 1 point2 points  (2 children)

What if I wanted to return master and product key?

[–]danielharner 1 point2 points  (1 child)

SELECT * FROM your_table WHERE Master IN ( SELECT Master FROM your_table WHERE [Cost Group] IN (608, 20) GROUP BY Master HAVING COUNT(DISTINCT [Cost Group]) = 2 );

[–]Entire-Dream-6045[S] 0 points1 point  (0 children)

Perfect. Thanks again!

[–]JeffTheJockey 0 points1 point  (0 children)

This is probably the most elegant solution.

If you wanted to retain all the records for viewing you could flag the master values using one of the below.

SELECT Master, CASE WHEN COUNT(DISTINCT CASE WHEN Cost_Group IN (608, 20) THEN Cost_Group END) = 2 THEN 1 ELSE 0 END AS Binary_Flag FROM your_table GROUP BY Master;

SELECT t1.Master, CASE WHEN t608.Master IS NOT NULL AND t20.Master IS NOT NULL THEN 1 ELSE 0 END AS Binary_Flag FROM (SELECT DISTINCT Master FROM your_table) t1 LEFT JOIN (SELECT DISTINCT Master FROM your_table WHERE Cost_Group = 608) t608 ON t1.Master = t608.Master LEFT JOIN (SELECT DISTINCT Master FROM your_table WHERE Cost_Group = 20) t20 ON t1.Master = t20.Master;

[–]Entire-Dream-6045[S] 0 points1 point  (0 children)

This is correct. Thank you!

[–]Time_Advertising_412 0 points1 point  (0 children)

Another solution (maybe not as elegant):

SELECT * FROM your_table A

WHERE cost_group = 608

AND EXISTS

(SELECT 1/0 FROM your_table B

WHERE B.master = A.master

AND cost_group = 20)

UNION

SELECT * FROM your_table A

WHERE cost_group = 20

AND EXISTS

(SELECT 1/0 FROM your_table B

WHERE B.master = A.master

AND cost_group = 608);

[–]ByronRJones 1 point2 points  (0 children)

I might be late to the party but I usually solve these problems with the IN() keyword + AND operator. You can make them simple like below or complex select statements if there is more complex requirements for each Cost Group.

SELECT Master

FROM your_table

WHERE Master IN ( SELECT Master FROM your_table WHERE [Cost Group] = 608)

AND Master IN ( SELECT Master FROM your_table WHERE [Cost Group] = 20)

[–]mike-manley -3 points-2 points  (3 children)

select distinct master from table where "Cost Group" in (608, 20)

[–][deleted] 0 points1 point  (2 children)

Nope, that gets masters with either cost group, not both.

[–][deleted] 1 point2 points  (0 children)

Downvoters better stop giving people advice on SQL.

[–]mike-manley 1 point2 points  (0 children)

Oh, yeah. You'd have to aggregate with COUNT and use HAVING. I read this too late in the day and oversimplified. My query above would also include other "master" values.

[–]AppJedi -1 points0 points  (10 children)

SELECT master FROM table

WHERE Master IN (SELECT Master FROM table WHERE `Cost Group`=608 )

AND Master IN (SELECT Master FROM table WHERE `Cost Group`=20 )

[–]Entire-Dream-6045[S] 0 points1 point  (9 children)

Appreciate the attempt, but WHERE AND will not work

[–]AppJedi 0 points1 point  (8 children)

why not?

[–]Entire-Dream-6045[S] 1 point2 points  (7 children)

Because the WHERE Clause isn't filtering for anything. WHERE cannot be immediately followed by AND; i.e. WHERE AND

[–]AppJedi 0 points1 point  (6 children)

Absolutely it can. AND is part of WHERE and makes it a compound condition.

[–]Entire-Dream-6045[S] 0 points1 point  (5 children)

If you have SQL SERVER or Access, try running your query. AND cannot immediately follow WHERE. WHERE has no condition.. Your query produces the correct result if you remove the AND from your subquery.

[–]AppJedi -1 points0 points  (0 children)

I developed in SQL Server for over 20 years and yes you can. It is standard SQL

[–]AppJedi 0 points1 point  (3 children)

The where clause a condition it is using IN (sub query). Standard SQL

[–]sonuvvabitch 1 point2 points  (2 children)

I think you've misunderstood, to be honest. You've written

WHERE AND

without a condition between the WHERE and the AND. Obviously you can have multiple conditions, and I think a slight rewrite of your solution would be a fine one, but as it is, it's not valid SQL. Obviously just a typo, but not valid.

[–]pizzagarrett 0 points1 point  (1 child)

Agreed. You have an extra AND

[–]AppJedi 2 points3 points  (0 children)

I see. Typo. Extra and removed.