|
I have a table that keeps tracks of all errors logged for an application I am running. This table includes a column for the date the error occurred, a column for the time, and the error number. There can be several errors with the same error number logged. For example, the table can look like this:
I am trying to query a list of each error number and the most recent date and time it occurred. So my results should be:
I have tried 2 different queries. The first one is:
But my results include each error number, and the most current date and the most current time. But it does not neccessarily mean the date and the time are from the same record. It just get the most recent date and the most recent time for each error number. I also tried this query:
But my results only include 1 record that ahs the most current date and time and the time is based on the date. The second query works, but I need to get it for all error numbers and not just the one that is the most recent. Each record has a column called "LogText" which is a description of the error. How can I view the LogText for each of the most recent errors? I tried to just add LogText after the Select statement and I added it in the Group By part too. Let me know if you have any suggestions I also need to include the errormsg column that is in another table named Errors. Table Errors has a column for the ErrorNum and I need to get the ErrorMsg that matches up with each ErrorNum in the Errors table. Please let me know if you have any suggestions. Thanks, Jen
(comments are locked)
|
|
NEW RESPONSE (based on new requirement in comment): In this new answer, I have added the LogText field and an additional JOIN to add that field to the most recent entries. If there were multiple entries with the exact same date and time, then you would see more than 1 entry for the same ErrorNum in that case. I hope this is the desired result, otherwise the query could be altered to only display a maximum of 1 result per ErrorNum regardless of how many logs occurred at the MAX date and time. --here is the answer
--SQL Server 2008, full example --change the date and time types to datetime for SQL 2005 or 2000
--change the insert statement to individual inserts for SQL 2005 or 2000
--display the full resultset but concatenate the Date and Time fields
--now apply the GROUP BY and MAX functions
--clean up for the example
ORIGINAL RESPONSE: The trouble is you are MAXing the date and times separately. All you have to do is concatenate the LogDate and LogTime fields and then MAX that. --here is the answer
I am grateful to Scott White for his simple way to concantenate the fields. Since you have the date and time in separate fields, I went ahead and did my example in SQL Server 2008, however, I have made notations so you can easily alter the example for 2005 or 2000. Let me know if you need more help with this. --SQL Server 2008, full example
--change the insert statement to individual inserts for SQL 2005 or 2000
--display the full resultset but concatenate the Date and Time fields
--now apply the GROUP BY and MAX functions
--clean up for the example Great, that worked perfectly! and it was pretty simple! I just have one other question. Each record has a column called "LogText" which is a description of the error. How can I view the LogText for each of the most recent errors? I tried to just add LogText after the Select statement and I added it in the Group By part too. Thanks,
Oct 12 '09 at 07:28 PM
Jennifer
Hi Jennifer, I hope you also find this additional answer helpful and will mark this post accordingly. Thanks. Benjamin I tried to enter the new answer here but I ran out of allowed characters. Please find my new answer in my editted response.
Oct 12 '09 at 10:46 PM
Benjamin
Thanks! It works perfect. I appreciate your help!
Oct 13 '09 at 12:19 PM
Jennifer
Hi Benjamin, I just remembered one more part that I need the query to do. I need to include the errormsg column that is in another table named Errors. Table Errors has a column for the ErrorNum and I need to get the ErrorMsg that matches up with each ErrorNum in the Errors table. I added this additional request to my original question.
Oct 13 '09 at 01:54 PM
Jennifer
(comments are locked)
|
|
This code makes a few assumptions:
select errornum
--min/max doesn't matter here since just one value is returned by cross apply
,max(LogDate) LogDate
,max(LogTime) LogTime
from [Log] as l
cross apply (select top 1 LogDate, logTime
from [Log]
where errorNum = l.errorNum
order by LogDate desc, LogTime desc
) ca
group by errornum
(comments are locked)
|
|
Another method
(comments are locked)
|


Jennifer, if you have additional information, please update your question. You can use this comment to say that your question is updated. If one of these answers works for you please check it as the answer. Thanks!
I have updated my question. Please help me with the last paragraph I added. Thanks!
See my answer. You need to join the table error with the query I posted