I have a sql server query question. I have a table like below. Giving a parameter to stored procedure, I need to query mutual CustomFields in lists that I want. For example, if ListID is given 1 and 2 and 3, result table will have columns that gives me ‘FullName’ in that case, because only ‘FullName’ is in all three ID’s. I did that which solved the issue somehow, but looking for a better and precise practice. Thanks
SELECT DISTINCT(CustomField)
FROM CustomFields a
WHERE EXISTS (
SELECT count(*)
FROM CustomFields b
WHERE a.CustomField = b.CustomField
HAVING count(*)>2
)
ORDER BY a.CustomField
CustomField ListID
PhoneNumber 1
Unvan 1
FullName 2
Surname 2
Regiob 2
FullName 3
BirthPlace 3
FullName 1
Here’s a more common approach to find CustomFields that are in groups 1, 2 and 3:
No subquery required, and the
whereclause filters out uninteresting groups before thegroup by.