I found an article that was somewhat like what I was trying to do. The article was titled:
SQL Query - Find block of sequential numbers
Here is the article
What I have is slightly different. I am trying to create a 'phone sheet' report which lists all the given phone numbers for a customer. Sometimes, the customer will have blocks of hundreds of sequential numbers that are all the same (except for the number itself). There are other fields like description, type, and LDCarrier. What I need is to list all of the phone numbers and additional info (description, type, LDCarrier) but group all of the numbers that are sequential and have the same description, type, and LDCarrier. If we have a phone sheet that lists all 2500 phone numbers, you can imagine how long listing all of those numbers would be... I would like to group all sequential numbers who have all the same type, description, and LDCarrier.
In one case, there is a group of 100 phone numbers (example - 3098699700 through 3098699799) which have all the same description, type, and LDCarrier. However, 3098699777 has a different 'type' value. I would need 3098699700 through 3098699776 grouped, 3098699777 all by itself and 3098699778 through 3098699799 all grouped.
Any numbers that are not sequential and the other fields are different would still be listed, but they would all be on separate lines instead of being grouped.
From the example query below (from the question I am referring to above), this query will group all sequential numbers, but it is not able to group numbers with other fields that have the same values. I am trying to model my query after that one and I am just not able to do it. Here is the query used in the other question:
Any help would be greatly appreciated!
Thanks!
SQL Query - Find block of sequential numbers
Here is the article
What I have is slightly different. I am trying to create a 'phone sheet' report which lists all the given phone numbers for a customer. Sometimes, the customer will have blocks of hundreds of sequential numbers that are all the same (except for the number itself). There are other fields like description, type, and LDCarrier. What I need is to list all of the phone numbers and additional info (description, type, LDCarrier) but group all of the numbers that are sequential and have the same description, type, and LDCarrier. If we have a phone sheet that lists all 2500 phone numbers, you can imagine how long listing all of those numbers would be... I would like to group all sequential numbers who have all the same type, description, and LDCarrier.
In one case, there is a group of 100 phone numbers (example - 3098699700 through 3098699799) which have all the same description, type, and LDCarrier. However, 3098699777 has a different 'type' value. I would need 3098699700 through 3098699776 grouped, 3098699777 all by itself and 3098699778 through 3098699799 all grouped.
Any numbers that are not sequential and the other fields are different would still be listed, but they would all be on separate lines instead of being grouped.
From the example query below (from the question I am referring to above), this query will group all sequential numbers, but it is not able to group numbers with other fields that have the same values. I am trying to model my query after that one and I am just not able to do it. Here is the query used in the other question:
Code:
SELECT * FROM (SELECT first_consecutive = MIN(part_number), last_consecutive, length = last_consecutive - MIN(part_number) + 1 FROM (SELECT P1.part_number, MIN(P2.part_number) AS last_consecutive FROM PartNumbersAvailable AS P1 LEFT JOIN (SELECT P1.part_number FROM PartNumbersAvailable AS P1 LEFT JOIN PartNumbersAvailable AS P2 ON P2.part_number = P1.part_number + 1 WHERE P2.part_number IS NULL) AS P2 ON P2.part_number >= P1.part_number GROUP BY P1.part_number) AS P GROUP BY last_consecutive) AS P WHERE length >= 5 ORDER BY first_consecutive
Thanks!
Comment