Edit:
I realized I asked the question wrong. What I really meant to ask was, "given a set of values for [Column B]
, check if there is a value in [Column A]
that matches all of the set and no others."
I'm not sure if what I want to do has an official name, so searching for my question has been difficult.
Given a table with 2 columns and a list of values, I want to see if this combination (and only this combination) exists in the table.
For instance, given this table (assume it's the whole table):
|----------|----------|
| Column A | Column B |
|----------|----------|
| 12345 | abcde |
|----------|----------|
| 12345 | xyz |
|----------|----------|
| 12345 | abcd |
|----------|----------|
| 12345 | abbba |
|----------|----------|
And given this input parameter:
Declare @columnBs Varchar(Max) = '["abcde","xyz","abcd","abbba"]';
For that set, I want to return 12345
. So, basically, I want to run a check and see if any value in [Column A]
matches all of the values in [Column B]
to all of the values in @columnBs
AND NO OTHER VALUES.
Without a value for [Column A]
as a starting point, I'm having trouble even conceiving of a long-form solution.
If it helps to conceptualize this better, this is a solution for messaging where:
[Column A]
represents the thread's primary key[Column B]
represents a user assigned to the thread
So, if a new message comes in for a set of users, I want to see whether there is an existing thread for all of the users supplied by @columnBs
and no other users.
I read this that you need to find a value in ColumnA that corresponds to exactly, no more and no less, the same values in your query. So, you need to join the search values, ensure all of them exist for a single ColumnA, and then ensure that no more exist. You could do it by cross joining them, but for larger sets of data, that would have awful performance. This may be a little better:
This will produce just the value 1 as a result, because it matches exactly.