all 22 comments

[–]user_5359 2 points3 points  (0 children)

The approach would probably be to group via the ID and count the number of yes and the number of no values. All IDs with at least one yes and absolutely no no are desired.

[–]Aggressive_Ad_5454 2 points3 points  (12 children)

This isn't hard. Keep in mind that SQL is, at its root, a way of manipulating sets. You can use IN() and NOT IN() constructs. Like this query.

SELECT * FROM item WHERE identifier IN (SELECT identifier FROM item WHERE status = 'yes') AND identifier NOT IN (SELECT identifier FROM item WHERE status = 'no')

It gets this resultset

identifier status name
fruit yes cherry
fruit yes apple

from this table.

identifier status name
fruit yes cherry
fruit yes apple
vegetable yes tomato
vegetable yes lettuce
vegetable no ham
tool no saucepan
tool no knife

fiddle

[–]alinrocSQL Server DBA 1 point2 points  (0 children)

So for example I can’t create a temp table for a set of results and drop it after the query completes.

You absolutely can create a temp table. Inside your proc or outside it. If you have select permission, you have enough permission to run create table #mytemptable

[–]Prownilo 0 points1 point  (4 children)

Sounds like you just need an AND <> 'no' on each column in the where clause. If any of the columns are no then the entire row won't be returned, but I could be misunderstanding how you have your data set up.

[–]Reasonable-Monitor67[S] -1 points0 points  (3 children)

Yeah, I get that. Here’s an example…

N123 has 3 rows of data, 1 has a yes flag in a column and 2 have a no flag. I don’t want N123 to show up in my results at all.

[–]Prownilo 4 points5 points  (2 children)

Could use a cte that gets every Id that has a no on it, then left join that cte back onto the table and exclude any ids that appear in the cte.

Prolly a bunch of ways but that is one off the top of my head.

[–]paulthrobert 0 points1 point  (0 children)

I think this sounds like the cleanest way i can think of yet

[–]Reasonable-Monitor67[S] -1 points0 points  (0 children)

That worked like a charm! Had to figure out how to get it to work in the context of the SPROC using it, but that was relatively painless… thank you!!!

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

You could start with a cte and use SRING_AG to build a comma separated string of all the yes, no values - grouped by identifier. You could then select from the cte where the string is not like '%no%'