I have two fields (VNUM and ANUM) which may either have duplicate values between them or one of them is a null field. I'm using isnull so it will definitely return one of the values in a single column instead of two separate columns.
The problem is that some records will have multiple VNUM or ANUM values. I don't want multiple records returned. I want a distinct record returned for each name but want to show the top or max VNUM or ANUM value. It doesn't really matter which one as long as I get one value returned from one of those fields.
Thanks.
Code:
SELECT DISTINCT PI.PIID, PI.SSN, PI.LastName, PI.FirstName, convert(varchar(10), PI.DOB, 101) as DOB, isnull(V.VNUM, V.ANUM) AS VA_NUM
FROM PI
LEFT OUTER JOIN P
ON PI.PIID = P.PIID
LEFT OUTER JOIN V
ON P.PID= V.PID
WHERE PI.SSN IS NULL
GROUP BY PI.SSN, PI.LastName, PI.FirstName, PI.DOB, V.VNUM, V.ANUM
ORDER BY PI.LastName
The problem is that some records will have multiple VNUM or ANUM values. I don't want multiple records returned. I want a distinct record returned for each name but want to show the top or max VNUM or ANUM value. It doesn't really matter which one as long as I get one value returned from one of those fields.
Thanks.