Reputation: 13921
Couldn't think of a better title for this question the time being, but here's a sample of the data set I'm working with:
Token Buyer Amount
---------------------------------------------------------------
F3D4A490-03C9-450E-873B-3B70E68FA65B Company B 1.50
F3D4A490-03C9-450E-873B-3B70E68FA65B Company C 0.99
A85677D8-C5A9-4766-8628-AADD769240C2 Company A 3.14
A85677D8-C5A9-4766-8628-AADD769240C2 Company B 2.44
FB72BEE1-194C-48D3-8EC1-669776E8924C Company C 2.01
E9FFACBF-F9B1-4278-B49D-42E605F21D28 Company B 1.25
I need to get a list of Token values that match one of the following two conditions:
In this case, my desired result set would contain the following:
F3D4A490-03C9-450E-873B-3B70E68FA65B
FB72BEE1-194C-48D3-8EC1-669776E8924C
E9FFACBF-F9B1-4278-B49D-42E605F21D28
Getting a list based on having a single token is easy enough. I can do a GROUP BY with a HAVING COUNT(*) = 1. It's the second condition that's giving me some trouble.
Performance is not a consideration for this solution, it's a one-off query that I'll be running.
Upvotes: 2
Views: 76
Reputation: 18411
SELECT Token
FROM TableName
GROUP BY Token
HAVING COUNT(*) = 1
UNION ALL
SELECT Token
FROM TableName
WHERE Token NOT IN
(
SELECT Token
FROM TableName
WHERE Buyer = 'Company A'
)
GROUP BY Token
HAVING COUNT(*) > 1
Upvotes: 4