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.
6 6627
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?
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. -
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;
-
Here's the two queries combined into one (even though it seems as you won't be coming back to see this.): -
SELECT Asset, Max(Rank) AS MaxOfRank
-
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
-
GROUP BY Asset;
-
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!
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; - Public Function ConsecutiveRecs(ByVal strComponent As Variant) As String
-
-
Dim RS As New ADODB.Recordset
-
Dim strExceptionLevel As String
-
Dim strAssetComponent As String
-
Dim strSQL As String
-
Dim intCounter As Long
-
-
-
' If IsNull(str) Then Exit Function
-
-
intCounter = 0
-
strExceptionLevel = "Initial"
-
strAssetComponent = "New"
-
-
strSQL = "SELECT Asset,Result, Date from RESULTS WHERE Asset'" & _
-
strComponent & "' ORDER BY Asset, Date;"
-
-
-
Set rs1 = Nothing
-
With RS
-
'----------- Open recordset to get records
-
.ActiveConnection = CurrentProject.Connection
-
.CursorType = adOpenForwardOnly
-
.LockType = adLockReadOnly
-
.Open strSQL
-
-
'--------loop through retrieved records
-
While Not .EOF
-
-
-
If strAssetComponent = ![Asset] And strExceptionLevel = ![Result] Then
-
intCounter = intCounter + 1
-
ConsecutiveRecs = intCounter
-
'---- respond with the count
-
Else
-
If strAssetComponent <> ![Asset] Then _
-
intCounter = 0
-
ConsecutiveRecs = intCounter
-
'---- reset the counter and respond with the count
-
-
End If
-
'---store current asset and component combination and exception level to compare with the next one (see above)
-
strExceptionLevel = ![Result]
-
strAssetComponent = ![Asset]
-
.MoveNext
-
-
Wend
-
.Close
-
End With
-
-
Set RS = Nothing
-
-
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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |