(°v°)
Main Topics
Browse All TopicsFollow up to previous question:
Original Question...
I have a dataset where I need to count the occurance of consecutive numbers. The challenge is that the same number can show up throughout the dataset, so you can't do a simple group by and count. Each row has a unique row ID that increments by 1.
It is simple to do in Excel with row+1 formulas, but in a database it has been tuff. I have been able to get close by joining the table to itself on the Unique ID +/- 1 to get the previous and next day values in one record, however that does not solve my objective of getting a count of consecutive values when the same value can show up more than once in separate places.
Here is a subset of the data:
ID COBDate Name Value
821902 11/27/06 Series1 7.008571667
821903 11/28/06 Series1 7.008571667
821904 11/29/06 Series1 7.008571667
821905 11/30/06 Series1 7.007347143
821906 12/01/06 Series1 7.008571667
821907 12/04/06 Series1 7.008571667
821908 12/05/06 Series1 7.008571667
should result in:
Count 1: 7.008571667 = 3
Count 2: 7.007347143 = 1
Count 3: 7.008571667 = 3
The accepted solution..
SELECT First(Value) As Val, Count(*) As N
FROM
(SELECT
A.ID,
A.Value,
(Select Top 1 ID From YourSample B
Where A.Value<>B.Value And B.ID<A.ID
Order by ID Desc
) AS grp
FROM YourSample A
) As C
GROUP BY grp
New Requirement... Attach the counts and a unique group ID back to the original datase. The group ID can be something like the first ID in the consecutive occurance. Looks something like this:
ID COBDate Name Value GroupID Count
821936 11/27/06 Series2 7.008571667 821936 3
821937 11/28/06 Series2 7.008571667 821936 3
821938 11/29/06 Series2 7.008571667 821936 3
821939 11/30/06 Series2 7.007347143 821939 1
821940 12/01/06 Series2 7.008571667 821940 3
821941 12/04/06 Series2 7.008571667 821940 3
821942 12/05/06 Series2 7.008571667 821940 3
Attached is an excel file to import to Access for testing.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: FlysterPosted on 2009-11-05 at 11:16:15ID: 25752847
Check attached DB and run AQuery. Is this the result you were looking for?
Flyster
Solution?