April 15, 2005 at 8:28 am
I've got 2 tables, one contains call records indexed by a callid, the 2nd table, which has that same callid contains journal entries related to the callid which are stored as a text datatype. In my select, i want to bring back the call record information from the first table based on callid, but only the last journal entry made to the journal table and not the full list of journal entries. I've tried using the MAX command, but SQL doesn't seem to like that on a text datatype, as i get the following error:
- The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
Here's what i have, which brings back every journal record:
SELECT
cl.CustID as Customer,
cl.CallStatus as Status,
RIGHT(cl.CallID, 4) AS HEAT,
cl.PVCS,
cl.Title,
cl.CallDesc AS Description,
cl.RecvdDate AS Date,
CONVERT(datetime,
CONVERT(varchar(10),j.EntryDate) + ' ' +
CONVERT(varchar(8),j.EntryTime)) AS JournalEntered,
j.EntryText AS Journal
FROM CallLog cl, Journal j
WHERE
cl.callid = j.callid
AND
cl.CallStatus IN ('Triage')
ORDER BY cl.CallStatus desc, cl.CustID, cl.CallID asc
Any suggestions are greatly appreciated.
thanks,
Ray
April 15, 2005 at 10:02 am
What in the Journal table determines the "lastest" entry and what is its column definition.
April 15, 2005 at 10:03 am
Divide and conquer
SELECT
cl.CustID as Customer,
cl.CallStatus as Status,
RIGHT(cl.CallID, 4) AS HEAT,
cl.PVCS,
cl.Title,
cl.CallDesc AS Description,
cl.RecvdDate AS Date,
JournalEntered,
j2.EntryText AS Journal
FROM
CallLog cl
left join
(select callid, max(EntryDate + EntryTime) JournalEntered
from Journal
group by callid ) lastj
on cl.callid = lastj.callid
left join
Journal j2
on lastj.callid = j2.callid
WHERE
cl.CallStatus IN ('Triage')
ORDER BY cl.CallStatus desc, cl.CustID, cl.CallID asc
* Noel
April 15, 2005 at 10:56 am
The Journal table has a unique datetime stamp on each journal entry as well as a unique journal identifier called heatseq.
Thanks for the reply, Noel. I tried your suggestion, however instead of getting just 1 record in the result set (which is what I would expect) i get 3 (because there are 3 journal entries).
Thanks for the responses, guys.
Ray
April 15, 2005 at 12:06 pm
Raymond,
I overlooked that! and I am not able to test it but this should "kill" my bug
SELECT
cl.CustID as Customer,
cl.CallStatus as Status,
RIGHT(cl.CallID, 4) AS HEAT,
cl.PVCS,
cl.Title,
cl.CallDesc AS Description,
cl.RecvdDate AS Date,
JournalEntered,
(select EntryText from Journal j2 where lastj.callid = j2.callid ) AS Journal
FROM
CallLog cl
left join
(select callid, max(EntryDate + EntryTime) JournalEntered
from Journal
group by callid ) lastj
on cl.callid = lastj.callid
WHERE
cl.CallStatus IN ('Triage')
ORDER BY cl.CallStatus desc, cl.CustID, cl.CallID asc
hth
* Noel
April 15, 2005 at 1:31 pm
That'll do it! Thanks, Noel!
April 15, 2005 at 1:33 pm
Cheers!
* Noel
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply