473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Compare consecutive records in Access

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;

Expand|Select|Wrap|Line Numbers
  1. ASSET    RESULT      DATE
  2. 310          Normal         12/03/2007
  3. 310          Warning       07/04/2007
  4. 310          Warning       17/05/2007
  5. 310          Normal         03/06/2007
  6. 310          Warning       04/07/2007
  7. 310          Normal         03/08/2007
  8. 311          Warning       12/03/2007
  9. 311          Warning       05/04/2007
  10. 311          Normal         16/05/2007
  11. 311          Normal         03/06/2007
  12. ...
  13.  
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.
Nov 1 '07 #1
6 6627
Rabbit
12,516 Expert Mod 8TB
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?
Nov 1 '07 #2
Rabbit
12,516 Expert Mod 8TB
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.  
Nov 1 '07 #3
Rabbit
12,516 Expert Mod 8TB
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.  
Nov 5 '07 #4
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!
Nov 18 '07 #5
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?
Nov 19 '07 #6
Rabbit
12,516 Expert Mod 8TB
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.
Nov 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Iwilfix | last post by:
I am completly new to Access. I need a field to automaticly enter invoice numbers that run consecutive. For example records starting with an invoice # 1276, would autmaticly go to #1277 and #1278...
2
by: Dennis Ruppert | last post by:
Greetings This should be easy, but I am stuck! I have a table that I import from another program. There are 25 fields, but I only need to use 3 of them for what I need to do. After I import...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
9
by: geronimo_me | last post by:
Hi, I am atempting to compare part of a field with the whole of another field in access. Is this possible? Basically I have 2 tables with the following info: Table1 Field1 = MR.
0
by: dfs9 | last post by:
In the article "Delete Duplicate Records From Access Tables" By Danny Lesandrini writes the following: This final suggestion is the most flexible and accurate. Given any table, it generates a...
4
by: dfs9 | last post by:
In the article "Delete Duplicate Records From Access Tables" By Danny Lesandrini writes the following: This final suggestion is the most flexible and accurate. Given any table, it generates a...
3
by: Nelson | last post by:
Hi All, I want to look at what happens to stock prices after a certain number of consecutive up or down days. Let's say, for instance, I'd like to see where a stock's price is 5 days after a...
13
by: Charlotte | last post by:
Hi, i've googeled to find a asp-script that can compare all the records in two different access databases the mdb's have exactly the same tables what i want is that (the output) all the...
8
by: gigonomics | last post by:
Hi all, I hope someone can help me out. I need to return the best available seats subject to the constraint that the seats are side by side (or return X consecutive records from a table column...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.