[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

Count consecutive numbers and the attach back to original dataset

Asked by BenBeall in Microsoft Access Database, SQL Query Syntax

Tags: SQL, Access, consecutive values, count

Follow 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.

Attachments:
 
Sample set of flat data
 
 
Related Solutions
Keywords: Count consecutive numbers and the att…
 
Loading Advertisement...
 
[+][-]11/05/09 11:29 AM, ID: 25752987Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: Microsoft Access Database, SQL Query Syntax
Tags: SQL, Access, consecutive values, count
Sign Up Now!
Solution Provided By: harfang
Participating Experts: 3
Solution Grade: A
 
[+][-]11/05/09 11:16 AM, ID: 25752847Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/05/09 11:56 AM, ID: 25753251Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]11/05/09 12:00 PM, ID: 25753294Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091118-EE-VQP-93 - Hierarchy / EE_QW_Related_20080208