We help IT Professionals succeed at work.
Get Started

SQL query to count records before they change

rwallacej asked
Last Modified: 2012-05-06

I have a problem with SQL query.   I want to display the first record and the number of times a value is duplicated before the next uniqu record. An example of input data

date field   value         other fields.....
01:00       1234        
01:01       1234
01:02       1234
01:03       654
01:04        321
01:05        321
01:06        1234

The results I would like to display:

Date field                           Value        Count of records
(first time value occurs)
01:00                                 1234              3
01:03                                 654              1
01:04                                 321              2
01:06                                 1234            1

Not that I don't want a count of e.g. 1234 over all record set but counts as the values change

Thank-you in advance for help in SQL.
Watch Question
Data Engineer
This problem has been solved!
Unlock 1 Answer and 26 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE