JohnnyLong
Programmer
Hi,
I have 2 tables, rc_Contacts and rc_Calls.
Each contact can have many calls.
rc_Calls has a CompletedFlag column of 1 or 0.
I need to select the last completed call and the last uncompleted call and display the results in one row.
Surname Subject MaxCallDate1 Subject MaxCallDate2
Smith No reply 15/04/2007 Spoke with.. 17/04/2007
Brown Call back 12/03/2007 NULL NULL
This is what I have so far:
SELECT co.Surname, ca.Subject, Max(ca.CallDate) AS MaxCallDate1, ca1.Subject, Max(ca1.CallDate) AS MaxCallDate2
FROM rc_Contacts co
JOIN rc_Calls ca
ON co.ContactID = ca.ContactID
LEFT OUTER JOIN rc_Calls ca1
ON co.ContactID = ca1.ContactID
WHERE co.BranchCode = '233'
GROUP BY co.Surname, ca.Subject, ca1.Subject, ca.CallDate, ca1.CallDate
ORDER BY co.Surname
However, this does not select the latest (by date) records, it selects all records, and where do I put the Where CompletedFlag = 0 / Where CompletedFlag = 1 ??
Any help much appreciated.
I am using SQL 7
John
I have 2 tables, rc_Contacts and rc_Calls.
Each contact can have many calls.
rc_Calls has a CompletedFlag column of 1 or 0.
I need to select the last completed call and the last uncompleted call and display the results in one row.
Surname Subject MaxCallDate1 Subject MaxCallDate2
Smith No reply 15/04/2007 Spoke with.. 17/04/2007
Brown Call back 12/03/2007 NULL NULL
This is what I have so far:
SELECT co.Surname, ca.Subject, Max(ca.CallDate) AS MaxCallDate1, ca1.Subject, Max(ca1.CallDate) AS MaxCallDate2
FROM rc_Contacts co
JOIN rc_Calls ca
ON co.ContactID = ca.ContactID
LEFT OUTER JOIN rc_Calls ca1
ON co.ContactID = ca1.ContactID
WHERE co.BranchCode = '233'
GROUP BY co.Surname, ca.Subject, ca1.Subject, ca.CallDate, ca1.CallDate
ORDER BY co.Surname
However, this does not select the latest (by date) records, it selects all records, and where do I put the Where CompletedFlag = 0 / Where CompletedFlag = 1 ??
Any help much appreciated.
I am using SQL 7
John