Connecting Tech Pros Worldwide Help | Site Map

Compare consecutive records in Access

Newbie
 
Join Date: May 2007
Posts: 3
#1: Nov 1 '07
Hi there, newbie to Access, I would normally use cursors in SQL Plus to do this type of thing, however I have been tasked with modifying an access 2000 application and am stuck on the VB code/query to perform the following actions.

I have a table RESULTS which has the following structure;

ASSET RESULT DATE
310 Normal 12/03/2007
310 Warning 07/04/2007
310 Warning 17/05/2007
310 Normal 03/06/2007
310 Warning 04/07/2007
310 Normal 03/08/2007
311 Warning 12/03/2007
311 Warning 05/04/2007
311 Normal 16/05/2007
311 Normal 03/06/2007
...

I am trying to find the maximum number of consecutive records (based on date) for each individual ASSET where the RESULT is WARNING, which for the above example would be 2 for 310 and 2 for 311 but am having real trouble. Any help would be appreciated as I really don't know where to start.
Rabbit's Avatar
Expert
 
Join Date: Jan 2007
Location: California
Posts: 3,835
#2: Nov 1 '07

re: Compare consecutive records in Access


I think you might be able to do it with a really complex query but would be a lot easier with VBA. Do you know any VBA?
Rabbit's Avatar
Expert
 
Join Date: Jan 2007
Location: California
Posts: 3,835
#3: Nov 1 '07

re: Compare consecutive records in Access


Well, since you're new to Access, you probably don't know any VBA.
This will be a two-query process, one to "rank" the records and the other to find the max. The second part is easy enough to do by yourself since you already have experience with SQL.

Expand|Select|Wrap|Line Numbers
  1. SELECT x.Asset, x.Result, x.Date, iif(x.Result='Warning',((SELECT Count(*) FROM Table2 WHERE Asset = x.Asset AND Result = 'Warning' AND Date < x.Date AND (SELECT TOP 1 Result FROM Table2 WHERE Asset = x.Asset AND [Date] < x.Date ORDER BY [Date] DESC;) = 'Warning';) + 1), 0) AS Rank
  2. FROM Table2 AS x;
  3.  
Rabbit's Avatar
Expert
 
Join Date: Jan 2007
Location: California
Posts: 3,835
#4: Nov 5 '07

re: Compare consecutive records in Access


Here's the two queries combined into one (even though it seems as you won't be coming back to see this.):
Expand|Select|Wrap|Line Numbers
  1. SELECT Asset, Max(Rank) AS MaxOfRank
  2. FROM (SELECT x.Asset, x.Result, x.Date, iif(x.Result='Warning',((SELECT Count(*) FROM Table2 WHERE Asset = x.Asset AND Result = 'Warning' AND Date < x.Date AND (SELECT TOP 1 Result FROM Table2 WHERE Asset = x.Asset AND [Date] < x.Date ORDER BY [Date] DESC) = 'Warning') + 1), 0) AS Rank FROM Table2 AS x) AS y
  3. GROUP BY Asset;
  4.  
Newbie
 
Join Date: May 2007
Posts: 3
#5: Nov 18 '07

re: Compare consecutive records in Access


Thanks Rabbit, sorry about the late reply, I have been on vacation (yay). I do know a little VB but I'll have a look at the script and see how I get on. Thanks again for your help!
Newbie
 
Join Date: May 2007
Posts: 3
#6: Nov 19 '07

re: Compare consecutive records in Access


OK tried the SQL statement but got the following error;

At most one record can be returned by this subquery. (Error 3354)

I have tried calling a function to loop through the data and increment a counter but it doesn't seem to work, here is the code;

Expand|Select|Wrap|Line Numbers
  1.  Public Function ConsecutiveRecs(ByVal strComponent As Variant) As String
  2.  
  3.     Dim RS As New ADODB.Recordset
  4.     Dim strExceptionLevel As String
  5.     Dim strAssetComponent As String
  6.     Dim strSQL As String
  7.     Dim intCounter As Long
  8.  
  9.  
  10.     ' If IsNull(str) Then Exit Function
  11.  
  12.     intCounter = 0
  13.     strExceptionLevel = "Initial"
  14.     strAssetComponent = "New"
  15.  
  16.     strSQL = "SELECT Asset,Result, Date from RESULTS WHERE Asset'" & _
  17.         strComponent & "' ORDER BY Asset, Date;"
  18.  
  19.  
  20. Set rs1 = Nothing
  21.     With RS
  22.         '----------- Open recordset to get records
  23.         .ActiveConnection = CurrentProject.Connection
  24.         .CursorType = adOpenForwardOnly
  25.         .LockType = adLockReadOnly
  26.         .Open strSQL
  27.  
  28.         '--------loop through retrieved records
  29.         While Not .EOF
  30.  
  31.  
  32.             If strAssetComponent = ![Asset] And strExceptionLevel = ![Result] Then
  33.                  intCounter = intCounter + 1
  34.                     ConsecutiveRecs = intCounter
  35.             '---- respond with the count
  36.             Else
  37.                 If strAssetComponent <> ![Asset] Then _
  38.                    intCounter = 0
  39.                 ConsecutiveRecs = intCounter
  40.             '---- reset the counter and respond with the count
  41.  
  42.             End If
  43.             '---store current asset and component combination and exception level to compare with the next one (see above)
  44.             strExceptionLevel = ![Result]
  45.             strAssetComponent = ![Asset]
  46.             .MoveNext
  47.  
  48.         Wend
  49.         .Close
  50.     End With
  51.  
  52.     Set RS = Nothing
  53.  
  54. End Function
I am calling this from the following query;

SELECT DISTINCT Asset , Result, Date, ConsecutiveRecs(Asset) AS Consecutives
FROM RESULTS
GROUP BY Asset, Result, Date;

The result of this is an error, INVALID USE OF NULL at the line strExceptionLevel = ![Result] any ideas?
Rabbit's Avatar
Expert
 
Join Date: Jan 2007
Location: California
Posts: 3,835
#7: Nov 19 '07

re: Compare consecutive records in Access


The SQL error is because you have an Asset that has multiple records with the same date.

The second error is because strings can not be null, you'll have to use the Nz() function to convert it to an empty string first.
Reply