How to get the previous row for a selection criteria

Reply

Join Date: Jan 2009
Posts: 2
Reputation: guest_user is an unknown quantity at this point 
Solved Threads: 0
guest_user guest_user is offline Offline
Newbie Poster

How to get the previous row for a selection criteria

 
0
  #1
Jan 22nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

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

 
0
  #2
Jan 22nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: guest_user is an unknown quantity at this point 
Solved Threads: 0
guest_user guest_user is offline Offline
Newbie Poster

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

 
0
  #3
Jan 22nd, 2009
Originally Posted by RamyMahrous View Post
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:
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

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

 
0
  #4
Jan 22nd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 60
Reputation: pclfw is an unknown quantity at this point 
Solved Threads: 5
pclfw pclfw is offline Offline
Junior Poster in Training

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

 
0
  #5
Jan 23rd, 2009
I am making an assumption or two here, such as LOGID, your primary key, is an IDENTITY column or similar, so try this

  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
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1543 | Replies: 4
Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC