I’ve tried looking for some information about a query I’m trying to write and come up with some ideas, but nothing solid enough to work with. I think I have a general idea about what I need to do but I’m just missing that little push, yeah?
What I’m trying to do is write a query that only gets records whose ‘type’ is exclusively one value, rejecting those where there might be several of the type I’m looking for but one different invalidates the entire set.
I realize that’s kind of confusing, so here’s an example:
LinkId LinkType
1234 A
1234 B
1235 A
1236 A
1236 A
Let’s say records of LinkType ‘A’ are what I’m interested in. If a LinkId has only ‘A’ records, I want it. If it has some (1 or many, it doesn’t matter) A records and a record of some other value, leave it alone. In the example table above, for example, the ID 1234 is out, but 1235 and 1236 are valid.
Now I know I can do something like
SELECT LinkId, LinkType
FROM dbo.ExampleTable
GROUP BY LinkId, LinkType
to collapse the table into something like
LinkId LinkType
1234 A
1234 B
1235 A
1236 A
but then I’m stuck. I figure I need to use a HAVING clause here, and suspect I need to use a COUNT or something to (maybe?) check for the number of rows, but I just can’t seem to bring the pieces together.
Any tips, or maybe some comments about what to search for?
T-SQL
Should do what you want.
So, grab the list of LinkIDs which are not of type A,
then grab the list of LinkIDs not in that previous selection (so only the As will show up)
You can’t do a Group By, because you would lose information about the LinkType when grouping by LinkID, so a SubQuery is the only way (and the most clear, in this situation).