I have tables:
Person
-------------
ID | name
Notes
---------------------------
targetID | Content | Date
CallHistory
--------------------------
CallerID | CalleeID | Date
Now say I have a Person and a Spouse (which is on the same Person table). They have corresponding notes and callhistory.
What I want is to Select the most recent date from either the spouse or person's most recent date (call or note)
Ive tried:
SELECT top 1 Date, ID from (select TargetID as ID, Date from notes
union
SELECT CalleeID as ID, Date from Callhistory)
WHERE ID in (person.ID, spouse.ID)
but without luck.
EDIT: This select is inside a select statement:
select p.*, SELECT top 1 Date, ID from (select TargetID as ID, Date from notes
union
SELECT CalleeID as ID, Date from Callhistory)
WHERE ID in (person.ID, spouse.ID) as RecentContactDate
From Person person
LEFT JOIN PersonRelationship pr on person.ID = pr.ID AND pr.Type = 3 -- spouse
LEFT JOIN Person spouse on pr.RelatedID = spouse.ID
......
Im getting Ambigous column name Date
error.
any ideas?