473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How do I return count of consecutive values in an ordered table?

Good Evening,

The following is a subset of a table I have in an MS SQL 2008 DB. I am trying to output to a table, the Serial Number , the LID with the greatest count of consecutive LID values, and the actual count. Important to note that the table is ordered by the Last Updated Date value descending (this condition is critical). It can be grouped by Serial Number or ordered by Serial Number ascending or descending...whatever is more efficient and makes sense. Here's what the data looks like:

[Serial Number] [LID] [Last Updated Date]

123456 AAA 2012-09-24

123456 AAA 2012-09-23

123456 AAA 2012-09-22

123456 AAA 2012-09-21

123456 BBB 2012-09-20

123456 BBB 2012-09-19

123456 AAA 2012-09-18

123456 AAA 2012-09-17

123456 AAA 2012-09-16

234567 BBB 2012-09-24

234567 BBB 2012-09-23

234567 AAA 2012-09-22

The desired output to the table is:

[Serial Number] [LID] [LID Count]

123456 AAA 4

234567 BBB 2

I am at a loss. I've tried using ROW_NUMBER() OVER(PARTITION BY [Service Tag], [LID] ORDER BY [Last Updated Date] DESC ) but all that does is break up my descending date order and I end up with the count and the LID that occurs the most during the range of dates.

Thanks in advance for any assistance you can provide!

Best Regards!
Sep 27 '12 #1
1 2584
Rabbit
12,516 Expert Mod 8TB
I can see it working with a very convoluted query in which you join the table to itself to get the row before the next break in LID values and then join to a itself a third time to do a ranking query, but SQL isn't really optimized for this kind of query.

You may be better off using a cursor so that you only have to make one pass through the records.
Sep 27 '12 #2

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

Similar topics

3
by: Larry Woods | last post by:
I have created a table that I want to fill with calculated values. I have given ID's to each cell. This must NOT be the way since I can't address these ID's. How do I store values in table cells...
10
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...
1
by: GIMME | last post by:
If I have : <input name="a1" value="1"> <input name="a2" value="3"> <input name="a3" value="2"> And I want the user to enter the values 1,2, and 3 but not necessarily in that order .... (The...
7
by: hjohnson | last post by:
Within the access environment, I have a table that I'd like to -add a column -place the record number of each record into that column The autonumber is not working for me because I am...
7
by: riceking | last post by:
Is it possible to do the following and if so, how? In one table, I have a list of email addresses I know are bad and no longer work. Another table has a list of companies with contact people,...
1
by: san1014 | last post by:
Hi I have a table SQL> select * from nodes; NODE_ID NODE_NAME -------------------- ------------------------------ N1 Kothhapet N2 Nagole...
1
newnewbie
by: newnewbie | last post by:
Hi, Could somebody please help me with VBA code to count unique values in a Report? Report is based on a query that has grouped values. Basically, I would like to use formula...
2
by: Cainnech | last post by:
Hi everyone, I have an array which has 7 values. I would like to check if they are consecutive. So this is the array: my_array = 2; my_array = 5; my_array = 6;
22
by: copleyuk | last post by:
Hi Everyone, I am trying to count the number of table entries which have the same . I am clearly missing something in the search criteria but cannot get the correct combination... Here is the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.