473,480 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

count consecutive wins per reg key

5 New Member
I need to count the number of consecutive wins(Fin Pos) per reg key from this data. Is it possible to do this with Access? I'm fairly new to Access queries.

Out of the larger group of data shown I would want:
Expand|Select|Wrap|Line Numbers
  1. "Reg Key" "Name"               "Consecutive Wins"
  2. 578664      CHANT'S ALIBHAI    10
  3. 614060      CHARGER BAR             3
Expand|Select|Wrap|Line Numbers
  1. "Reg Key" "Name"            "Index No" "Fin Pos"
  2. 578664      CHANT'S ALIBHAI    84374    7
  3. 578664      CHANT'S ALIBHAI    84802    4
  4. 578664      CHANT'S ALIBHAI    85070    1
  5. 578664      CHANT'S ALIBHAI    85734    1
  6. 578664      CHANT'S ALIBHAI    86173    1
  7. 578664      CHANT'S ALIBHAI    86381    1
  8. 578664      CHANT'S ALIBHAI    86858    1
  9. 578664      CHANT'S ALIBHAI    87066    1
  10. 578664      CHANT'S ALIBHAI    87679    1
  11. 578664      CHANT'S ALIBHAI    88592    1
  12. 578664      CHANT'S ALIBHAI    88771    1
  13. 578664      CHANT'S ALIBHAI    89058    1
  14. 578664      CHANT'S ALIBHAI    89219    2
  15. 578664      CHANT'S ALIBHAI    89507    1
  16. 578664      CHANT'S ALIBHAI    89926    8
  17. 578664      CHANT'S ALIBHAI    94420    6
  18. 614060      CHARGER BAR            88785    3
  19. 614060      CHARGER BAR            88878    6
  20. 614060      CHARGER BAR            89149    1
  21. 614060      CHARGER BAR            92813    4
  22. 614060      CHARGER BAR            93436    1
  23. 614060      CHARGER BAR            93894    1
  24. 614060      CHARGER BAR            94226    1
Aug 7 '13 #1
11 1634
Seth Schrock
2,965 Recognized Expert Specialist
Most things are possible, but you don't explain how you came up with the result that you are looking for. I personally see no correlation between the results you are wanting and the larger data group.

**Edit**
I think that I might see it now. The field Fin Pos is Finish Position and you are looking for consecutive records where the finish position is 1. Is that correct? Do you have a primary key field? Also, what do you want to happen if you have two groups of consecutive wins? For example, what if the record
Expand|Select|Wrap|Line Numbers
  1. 614060     CHARGER BAR     88878    6
were changed to a win so that you would end up with
Expand|Select|Wrap|Line Numbers
  1. 614060     CHARGER BAR     88785    3
  2. 614060     CHARGER BAR     88878    1
  3. 614060     CHARGER BAR     89149    1
  4. 614060     CHARGER BAR     92813    4
  5. 614060     CHARGER BAR     93436    1
  6. 614060     CHARGER BAR     93894    1
  7. 614060     CHARGER BAR     94226    1
Would you want the result set to be
Expand|Select|Wrap|Line Numbers
  1. 578664     CHANT'S ALIBHAI    10
  2. 614060     CHARGER BAR        3
  3. 614060     CHARGER BAR        2
Aug 7 '13 #2
skptigger
5 New Member
Yes that is right. I want to count how many consecutive finish positions of 1 for each horse. the number field is a number representing the horse(ex: Charger Bar's number is 614060) If there is more than one within a horse number then I would want to count both instances like you showed in your post.

I'm using Access 2010.
Aug 7 '13 #3
zmbd
5,501 Recognized Expert Moderator Expert
Seth,
Go back to the origninal post data, second block.
OP is counting records from 4 thru 13, because at 14 [CHANT'S ALIBHAI] "place" and then doesn't have a consecutive following that point.
Same thing with [CHARGER BAR], where OP is only counting records 22 thru 24.
If there is a subsequent series of wins then that is also usually recorded... this way they keep track of winning streaks.

Standard Horse race thing - watched my Mom's boss do this by hand in a little brown book :).
(sorry, recognize the names - however, I think these are very old stats, Chant's Alibhai raced in the mid to late '70s - yes? Breading in '80s or '90s - Seems I remember going to the races in DelMar and hearing the name in relation to a horse - been almost 30 years so I might be mis-remembering.)
Never tried to do a DB on it.
Aug 7 '13 #4
zmbd
5,501 Recognized Expert Moderator Expert
Looks Like I cross posted with skptigger
Aug 7 '13 #5
Seth Schrock
2,965 Recognized Expert Specialist
Yep, I got that now Z. Thank-you for the formatting too. Definitely one of those times where I wish I was a moderator and could do it myself.

I'm thinking that this is going to require a subquery possibly in a GROUP BY clause, but I don't have anything concrete in my mind yet. I'll keep working at it, though. Is there any date field or primary key value that would help the database know the order of the races? Or is it just the order of the entry?
Aug 7 '13 #6
skptigger
5 New Member
There is a race date field in the data I have,but I included the index # in this post. It is unique to each race and can be used to put the races in order by sorting on the it. If the race date is preferrable I can add it.
Aug 7 '13 #7
Rabbit
12,516 Recognized Expert Moderator MVP
There are two ways to do this. The VBA way and the SQL way.

The VBA way is to loop through an ordered recordset while counting and looking for a break in the wins before restarting the count. And after each break, you will have to compare it to the prior max. Once, you reach the end of a group, you output the max for that group.

The SQL way is to join the table to itself to get the records after it that are not first place for each record that is first place. Take the MIN() of the end range for each one. Then do a COUNT() for each end range. And finally get the MAX() for each group.
Aug 7 '13 #8
skptigger
5 New Member
Ok, I'm new to this forum and new to VBA. Is it possible/acceptable to get an example of the code for these?
@Rabbit
Aug 8 '13 #9
Rabbit
12,516 Recognized Expert Moderator MVP
It's not the policy to do the work for poster on this forum but since this is a little bit complex, I don't mind posting some pseudocode.

If you don't know VBA, then you'll want to learn the basics so you can translate the psuedocode into VBA code.
Expand|Select|Wrap|Line Numbers
  1. priorGroup = ""
  2.  
  3. For Each row In ordered recordset
  4.    If group != priorGroup
  5.       output group and MaxConsecutiveWins
  6.       priorGroup = group
  7.       MaxConsecutiveWins = 0
  8.       ConsecutiveWins = 0
  9.    End If
  10.  
  11.    If place = 1 Then
  12.       ConsecutiveWins += 1
  13.    Else
  14.       If ConsecutiveWins > MaxConsecutiveWins
  15.          MaxConsecutiveWins = ConsecutiveWins 
  16.          ConsecutiveWins = 0
  17.       End If
  18.    End If
  19.  
As for the SQL version, there's really no way to do psuedocode for that but here's a basic example implemented for SQL Server that you can translate to Access SQL.
Expand|Select|Wrap|Line Numbers
  1. declare @t table(groupField char(1), orderField int, placeField int)
  2. insert into @t
  3. select 'A', 1, 2 union all
  4. select 'A', 2, 1 union all
  5. select 'A', 3, 1 union all
  6. select 'A', 4, 1 union all
  7. select 'A', 5, 1 union all
  8. select 'A', 6, 1 union all
  9. select 'A', 7, 3 union all
  10. select 'A', 8, 1 union all
  11. select 'A', 9, 1 union all
  12. select 'A', 10, 1 union all
  13. select 'B', 12, 2 union all
  14. select 'B', 13, 1
  15.  
  16.  
  17. SELECT
  18.     x.groupField,
  19.     MAX(x.ConsecutiveWins) AS maxConsecutiveWins
  20.  
  21. FROM
  22.     (
  23.         SELECT 
  24.             t.groupField,
  25.             COUNT(*) AS ConsecutiveWins
  26.  
  27.         FROM 
  28.             (
  29.                 SELECT 
  30.                     t1.groupField,
  31.                     t1.orderField,
  32.                     MIN(
  33.                         CASE 
  34.                             WHEN t2.orderField IS NULL
  35.                                 THEN -1
  36.                             ELSE t2.orderField
  37.                         END
  38.                     ) AS EndRange
  39.  
  40.                 FROM 
  41.                     @t AS t1
  42.  
  43.                     LEFT JOIN @t AS t2 ON
  44.                         t1.groupField = t2.groupField AND
  45.                         t1.orderField < t2.orderField AND
  46.                         t2.placeField <> 1
  47.  
  48.                 WHERE
  49.                     t1.placeField = 1
  50.  
  51.                 GROUP BY
  52.                     t1.groupField,
  53.                     t1.orderField
  54.             ) t
  55.  
  56.         GROUP BY
  57.             t.groupField,
  58.             t.EndRange
  59.     ) x
  60.  
  61. GROUP BY x.groupField
Aug 8 '13 #10
skptigger
5 New Member
Don't worry it's not for work. It is just something that I wanted to do on my own. Thanks for the examples.
Aug 9 '13 #11
Rabbit
12,516 Recognized Expert Moderator MVP
I didn't mean work as in a job. I only meant the general sense of work. If I just post fully working code, there's no incentive to understand how and why it works. And if you don't understand the solution, you won't be able to adapt it if the circumstances change a little. It's the whole teach a person to fish thing.

Let us know how you get along with transforming the examples to fit your situation.
Aug 9 '13 #12

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

Similar topics

8
3000
by: Adam | last post by:
Hi, I am trying to mark consective numbers in a data set and get a count as to how many consecutive numbers exist in the a given line of data. Here is an example line: 3, 5, 7, 9, 10, 13,...
10
5944
by: Walter Brunswick | last post by:
Is there any way to iterate through a sequence of characters to find N consecutive equivalent characters? So, for example, the string "taaypiqee88adbbba" would return 1 if the number (of...
10
6653
by: ChrisD | last post by:
I'm trying extract a count of consecutive numbers, or "unbroken" years in this case, at any particular given time. For example (simplified): CREATE TABLE #Customers ( CustNo INT, YearNo...
9
2752
by: MLH | last post by:
A mailing list table in its virgin state contained sequential, consecutive integers in an autonumber field (A97). I've deleted records throughout the table. Now I would like to identify each...
6
2910
by: cesco | last post by:
Hi, say I have a vector like the following: vec = and I'd like to know via a function (for example, ConsecutiveIntegers(vec, N)) whether there are N consecutive integers. So, for example,...
8
12904
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...
8
5324
by: TasmanianDevil | last post by:
Can anyone help me with a Formula in VB/excel to COUNT how many numbers are consecutive within a Row that spans 20 Columns? Example: 65 67 68 69 75 79 80 84 85 90 78 73 61 93 92 91 95 6 33 99 ...
11
4788
wordbrew
by: wordbrew | last post by:
Hello all, I’m beginning to get a pretty good foundation in VBA, but I can’t wrap my head around this problem, even after trolling around the net searching through other posts that seemed remotely...
0
1211
by: Kim Mitchell | last post by:
I've been asked to create a report that shows the days a person has called out of the office. If they have called out more than 1 day in a consecutive time frame, they should be appearing on the...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6911
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7091
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...
1
6743
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
5344
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
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.