I thought this would be an easy query - but it isn't.
This is a support call tracking app. that we have running here. We have support calls and notes that are continually added to them throughout the day. I want to be able to query the list of open support calls along with the information on the last support call note. I can do it by using my web code (ColdFusion in case anyone cares) to loop queries, but that means running tons and tons of queries. I'd really like to return the results in a single query. Here's what I have so far:
Tables:
tblSupportCalls
SupportCallID
EmployeeID
SupportCallDesc (Not needed in this query.)
SupportCallStatus
tblSupportCallNotes
SupportCallNoteID
SupportCallID
EmployeeID
SupportCallNoteTime
SupportCallNoteDesc (Not needed in this query.)
Code:
SELECT tblSupportCalls.SupportCallID,
tblSupportCalls.EmployeeID,
tblSupportCallNotes.EmployeeID AS LastEmployeeId,
tblSupportCallNotes.SupportCallNoteTime AS LastSupportCallNoteTime
FROM tblSupportCalls LEFT OUTER JOIN
tblSupportCallNotes ON
tblSupportCalls.SupportCallID = tblSupportCallNotes.SupportCallID
WHERE (tblSupportCalls.SupportCallStatus = 0)
AND
(
tblSupportCallNotes.SupportCallNoteTime IN
(SELECT MAX(SupportCallNoteTime)
FROM tblSupportCallNotes
GROUP BY SupportCallID)
OR
tblSupportCallNotes.SupportCallNoteTime IS NULL
)
ORDER BY tblSupportCalls.SupportCallID DESC
The problem with this query is that it is returning multiple records and I can't figure out why. I don't know if this is important, but we are using SQL Server 2000.
Thanks in advance for anyone who can shed a little light on my problem.
—Supes