| | |
How to get the previous row for a selection criteria
Please support our MS SQL advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2009
Posts: 2
Reputation:
Solved Threads: 0
We have a Log table which logs the events of a request.
When any error event occurs, I need a query to see the row with errorevent and the previous row to this row which contains the actual data which caused the error.
Here is the table structure:
LOGID (int), --> primary key
EventId (int),
Description (varchar (max))
Logtime (datetime)
Any help is greatly appreciated..
Thanks
When any error event occurs, I need a query to see the row with errorevent and the previous row to this row which contains the actual data which caused the error.
Here is the table structure:
LOGID (int), --> primary key
EventId (int),
Description (varchar (max))
Logtime (datetime)
Any help is greatly appreciated..
Thanks
Give me sample data and what you need to get, then I can understand you well
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Jan 2009
Posts: 2
Reputation:
Solved Threads: 0
•
•
•
•
Give me sample data and what you need to get, then I can understand you well
In the next step, we validate field in the the request and log an errorevent with eventid=4 and the error message as description .
Now I want to see the description data (which contains actual request data) from the previous row to the errorevent record.
Here is the sample data:
•
•
•
•
Here is the sample data:
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Jun 2005
Posts: 60
Reputation:
Solved Threads: 5
I am making an assumption or two here, such as LOGID, your primary key, is an IDENTITY column or similar, so try this
Add another AND clause to get only the latest event records for the relevant event code.
SQL Syntax (Toggle Plain Text)
SELECT e.logid, e.eventid, e.description, e.logtime, l.logid, l.eventid, l.description, l.logtime FROM logtable e, logtable l WHERE e.eventid = <event id that you want to examine> AND l.logid = (e.logid - 1) ORDER BY e.logtime DESC, e.logid DESC
Add another AND clause
SQL Syntax (Toggle Plain Text)
AND e.logtime = (SELECT max(logtime) FROM logtable WHERE eventid = <event id that you want to examine>
Last edited by pclfw; Jan 23rd, 2009 at 9:29 am.
![]() |
Other Threads in the MS SQL Forum
- Previous Thread: Find out why a transaction (DTS) failed
- Next Thread: Copying only the records that don't already exist in destination table.
Views: 1525 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for MS SQL
"last autogrowth business connectingtodatabaseinuse count cursor data database dateadd datepart day" dbsize deadlock delete_trigger exploit getdate hack highperformancecomputing hpc hpcserver2008 ibm iis limit loop maximum microsoft ms mssql multiple multithreading news number password permission position query reporting result security server services sets single source sql sql-injection sqlserver sqlserver2005 subtype supercomputing supertype tables uniqueid update view weekday






