943,852 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 5297
  • MS SQL RSS
Jan 22nd, 2009
0

How to get the previous row for a selection criteria

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
guest_user is offline Offline
2 posts
since Jan 2009
Jan 22nd, 2009
0

Re: How to get the previous row for a selection criteria

Give me sample data and what you need to get, then I can understand you well
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 22nd, 2009
0

Re: How to get the previous row for a selection criteria

Give me sample data and what you need to get, then I can understand you well
when I a get a request, we log the event with eventid =1 and request data in the description field
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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
guest_user is offline Offline
2 posts
since Jan 2009
Jan 22nd, 2009
0

Re: How to get the previous row for a selection criteria

Quote ...
Here is the sample data:
???!
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Jan 23rd, 2009
0

Re: How to get the previous row for a selection criteria

I am making an assumption or two here, such as LOGID, your primary key, is an IDENTITY column or similar, so try this

SQL Syntax (Toggle Plain Text)
  1. SELECT e.logid, e.eventid, e.description, e.logtime,
  2. l.logid, l.eventid, l.description, l.logtime
  3. FROM logtable e, logtable l
  4. WHERE e.eventid = <event id that you want to examine>
  5. AND l.logid = (e.logid - 1)
  6. ORDER BY e.logtime DESC, e.logid DESC

Add another AND clause
SQL Syntax (Toggle Plain Text)
  1. AND e.logtime = (SELECT max(logtime) FROM logtable WHERE eventid = <event id that you want to examine>
to get only the latest event records for the relevant event code.
Last edited by pclfw; Jan 23rd, 2009 at 9:29 am.
Reputation Points: 33
Solved Threads: 9
Junior Poster
pclfw is offline Offline
132 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Find out why a transaction (DTS) failed
Next Thread in MS SQL Forum Timeline: Copying only the records that don't already exist in destination table.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC