I have a database table that looks like:
ForeignId: int
Key: varchar
Value: varchar
Where ForeignId and Key constitute a unique primary key
I can easily determine the total set of keys defined for a given set of document with
SELECT DISTINCT [Key] FROM [Table] WHERE [ForeignId] IN (...)
What I would like to do however is to further distinguish the values of each values of each property in the case where it is the same for every ForeignId (and NULL or some other sentinal value in the case where there the values differ).
If I do something like:
ForeignId Key Value 1 1 A 1 2 B 1 3 C 2 1 A 2 2 Z 3 1 A 3 2 Z
I want output like:
Key Value 1 A -- All 3 are the same 2 NULL -- More than one distinct value (B, and Z) 3 NULL -- Defined for only one ForeignId
The best I have been able to come up with is
SELECT [Key], MAX([Value]), MIN([Value]) FROM [Table]
WHERE [ForeignId] IN (...)
GROUP BY [Key]
and looking for any instances where the max and min values returned differ. If they are the same I assume all of the values match, if they differ, I know there are more than one distinct values.
What's missing here is the 3rd part where I need to mark values as differing if any of the individual items doesn't have a definition at all. In my example above, my current code would return the value C for key 3 even though it is not defined for some of the ForeignIds.