Skip to content
Advertisement

MS Access SQL query – Count records until value is met


I have an Access query (qr1) that returns the following data:

dateField stringField1 stringField2 booleanField
11/09/20 17:15 John Nick 0
12/09/20 17:00 John Mary -1
13/09/20 17:30 Ann John 0
13/09/20 19:30 Kate Alan 0
19/09/20 19:30 Ann Missy 0
20/09/20 17:15 Jim George 0
20/09/20 19:30 John Nick 0
27/09/20 15:00 John Mary -1
27/09/20 17:00 Ann John -1
27/09/20 19:30 Kate Alan 0
28/09/20 18:30 Ann Missy -1
03/10/20 18:30 Jim George -1
04/10/20 15:00 John Nick 0
04/10/20 17:15 John Mary 0
04/10/20 20:45 Ann John 0
05/10/20 18:30 Kate Alan 0
17/10/20 15:00 Jim George 0
17/10/20 17:15 John Nick 0
18/10/20 15:00 John Mary -1
18/10/20 17:15 Ann John 0

Notes:

  • The string data may by repetitive or not.
  • The date data are stored as string. I use a function to convert it as date.
  • qr1 is ORDERED BY STR2TIME(dateField) ASC

Now I need to run an extra query that will do the following:

  • add an extra column where:
  • counts records until yes (-1) on booleanField
  • after this, starts over counting by 1

In this case the output should look like this:

dateField stringField1 stringField2 booleanField countField
11/09/20 17:15 John Nick 0 1
12/09/20 17:00 John Mary -1 2
13/09/20 17:30 Ann John 0 1
13/09/20 19:30 Kate Alan 0 2
19/09/20 19:30 Ann Missy 0 3
20/09/20 17:15 Jim George 0 4
20/09/20 19:30 John Nick 0 5
27/09/20 15:00 John Mary -1 6
27/09/20 17:00 Ann John -1 1
27/09/20 19:30 Kate Alan 0 1
28/09/20 18:30 Ann Missy -1 2
03/10/20 18:30 Jim George -1 1
04/10/20 15:00 John Nick 0 1
04/10/20 17:15 John Mary 0 2
04/10/20 20:45 Ann John 0 3
05/10/20 18:30 Kate Alan 0 4
17/10/20 15:00 Jim George 0 5
17/10/20 17:15 John Nick 0 6
18/10/20 15:00 John Mary -1 7
18/10/20 17:15 Ann John 0 1

Problem

I have tried many things all giving wrong numeric results.

Finally I thought that counting the zeros from the current date till the previous (biggest and smaller than the current), would do the trick:

but still gives me wrong numeric results on countField:

countField
0
0
1
2
3
4
5
6
1
2
3
1
12
13
14
15
16
17
18
13

What am I doing wrong? I can’t get it. How to get the desired result?


EDIT:

I’m posting the final code, based on @Gordon Linoff ‘s and @Gustav ‘s answers, slightly simplified.

Explanation of changes:

  • I got rid of the conversion-function in this step. Instead of converting 7 times * every single record, I convert only once in the first query and here the values are ready to compare.
  • I omitted checking the zeros as it was not necessary.
  • I added NZ function to get values when the inner subquery returns NULL. That is when there isn’t any yes with smaller date to count from (first records usually).
  • The only problem left, was that with NZ I got values 1 less than what I needed, so I added -1 to the dateField to count 1 more.

Here is the code:

Advertisement

Answer

This is doable, though a little convoluted:

Output:

enter image description here

You string converter can be replaced by this expression:

This you should apply at a much earlier state, like in qr1.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement