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 http://bytes.com/topic/sql-server/an...ential-numbers
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: - 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
Any help would be greatly appreciated!
Thanks!
18 2426 ck9663 2,878
Recognized Expert Specialist
When you say..
I would need 3098699700 through 3098699776 grouped, 3098699777 all by itself and 3098699778 through 3098699799 all grouped.
Do you mean you just need an identifier that group them all together? Or you want it to be in a single row?
~~ CK
Thanks for helping.
What the example does for me (and that part works) is that it gives me a row for each group, starting with first_consecuti ve and then last consecutive and then the length.
In the example given it would return the following:
first_consecuti ve Last consecutive length type
3098699700 3098699776 77 ThisType
3098699777 3098699777 01 ThatType
3098699778 3098699799 22 ThisType
I hope this helps.
Thanks!
ck9663 2,878
Recognized Expert Specialist
What would happen if there's a gap on the sequence but they have the same type?
~~ CK
This is an interesting requirement, I'll see if I can help.
That would be a new record. Only sequential phone numbers with exactly the same type, description, and LDCarrier would be grouped.
Thanks!
b0010100,
That does not group sequential numbers at all. That only groups numbers with the same carrier. I will sometimes have thousands of numbers and I need to group any sequential numbers that have the same LDCarrier, type and description together.
If I had the number 9999999999 in that table with a Carrier of 'Carrier B' you code would have added that to the middle record and there would have been two numbers with Carrier B.
Thanks!
Joel,
You're 100% right, I jumped the gun on my solution. I though I pulled it back in time but I guess I didn't. Here's something that may work better for you: - DECLARE @tbl TABLE(number Bigint,[type] char(10),[description] char(20),Carrier char(10))
-
DECLARE @t2 TABLE(number Bigint,[type] char(10),[description] char(20),nSet int,Carrier char(10))
-
DECLARE @c BIGINT
-
DECLARE @nSet INT
-
DECLARE @pNum BIGINT
-
DECLARE @num bigint
-
DECLARE @cName char(10)
-
Declare @desc char(10)
-
DECLARE @type char(10)
-
-
SET @pNum = 0
-
SET @nSet = 0
-
-
SET @c = 3098699700
-
WHILE @c <= 3098699776
-
BEGIN
-
INSERT INTO @tbl VALUES(@c,'East','My Descript','Carrier A');
-
SET @c = @c + 1
-
END
-
-
SET @c = 3098699777
-
WHILE @c < =3098699777
-
BEGIN
-
INSERT INTO @tbl VALUES(@c,'West','My Descript','Carrier B')
-
SET @c = @c + 1
-
END
-
-
SET @c = 3098299777
-
WHILE @c < =3098299777
-
BEGIN
-
INSERT INTO @tbl VALUES(@c,'South','My Descript','Carrier B')
-
SET @c = @c + 1
-
END
-
-
SET @c = 3098699778
-
WHILE @c <= 3098699799
-
BEGIN
-
INSERT INTO @tbl VALUES(@c,'North','My Descript','Carrier C')
-
SET @c = @c + 1
-
END
-
-
DECLARE cr CURSOR FOR
-
SELECT * FROM @tbl ORDER BY [number]
-
-
OPEN cr
-
-
FETCH NEXT FROM cr
-
INTO @num,@type,@desc, @cName
-
-
-
WHILE @@FETCH_STATUS = 0
-
BEGIN
-
-
if NOT @num = @pNum + 1
-
BEGIN
-
print cast(@num as varchar(12)) + ' - ' + cast(@pnum as varchar(12))
-
SET @nSet = @nSet + 1
-
END
-
-
INSERT INTO @t2 (number,[type],[description],nSet,Carrier) VALUES (@num,@type,@desc,@nSet,@cName)
-
-
SET @pNum = @num
-
-
FETCH NEXt FROM cr
-
INTO @num,@type,@desc, @cName
-
END
-
-
CLOSE cr
-
DEALLOCATE cr
-
-
-
SELECT min(number),max(number),count(*),Carrier
-
FROM @t2
-
GROUP BY nSet,[type],[description],Carrier
ck9663 2,878
Recognized Expert Specialist
What's the maximum number, in sequence, that a company have? Thousands, meaning the last 4 digits are the only unique portion? Ten thousands, meaning the last 5 digits are the only unique portion?
~~ CK
ck9663,
I am not sure what your question is exactly but I think you are asking what is the maximum a company could have. this is for 10 digit phone numbers so theoretically it could be numbers between 0000000000 and 9999999999 (Although I don't think those numbers exist or those area codes exist.). However, I do need to accomodate for any numbers within that range. The are any 10 digit numbers. They could have a range of 300 consecutive numbers, 10, 500 non-consecutive numbers, etc... Anyone can pick any numbers they want. Although if we can, we try to use consecutive blocks of numbers for them to keep things simple.
We want to list all of their phone numbers and associated information about those numbers, but in an effort to keep the list as short as possible, I would like to group together any consecutive numbers with the same values.
Thanks!
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Jenn L |
last post by:
I have a database that is pre-populated with sequential part numbers.
As people reserve the parts I update a flag to show the # is no longer
available. Now they want the ability to take out a block of "x"
number of sequential part numbers - say for example 5.
If my database had the following numbers available:
101
104
105
110
|
by: Ken |
last post by:
This is a challenge. Perhaps someone can offer suggestions.
I am trying to create a variable, ordernumber, that increases by an
increment of 1 every time the variable is accessed.
For example:
Call up the variable the first time: ordernumber = 1
"Reopen" the webpage, call up the variable a second time: ordernumber = 2,
etc.
|
by: Tony Williams |
last post by:
I recently posted a message asking for help with
sequential numbers. I want to create an autonnumber
reference number that reverts back to 1 at the start of
each year. GlenAppleton gave me some code and I thought I
had it working but I don't. When I create a new record
nothing happens in the control CommDocNbrtxt where the
number should appear.
Here is my complete code, can anyone help?
If you read this Glen sorry to post again but I...
|
by: amywolfie |
last post by:
Hi All:
I know this is simple, but I just can't seem to get there:
I need to sort a table by a text field (txtDescription), then assign
sequential numbers to the field SEQUENCE in table.
Every time a new record is added, the all SEQUENCE #'s must be reset.
The re-numbering will be triggered by an EXPORT button.
|
by: Mike Kingscott |
last post by:
Hi all,
I'd building an app that posts to a web service. One of the things
that is required in the soap header is a sequential number appended to
a ref, i.e. "IGI1001", "IGI1002", etc.
Obviously the first part of the reference ("IGI") stays the same, but
I need an incrementing number generated. Now, I was thinking of doing
this via a SQL database, but it would be an awful waste as there would
only be one table and one stored procedure...
| |
by: John |
last post by:
Hi
I need to assign sequential invoice numbers to orders starting from the last
highest number + 1. I have tried the following code;
UPDATE Orders SET Orders. = DMax("","Orders")+1
WHERE Orders.) Is Null AND ...
The problem is that all orders get the same number which is the last highest
number + 1. Apparently the query does not recalculate DMax("","Orders")+1 for each record and instead only gets the value once in the
|
by: Bruce |
last post by:
Surely someone has done this before, and I am guessing there is a
simple solution that is eluding me. I have a simple report based on a
recordset. For each record there is a field (RecNum) that is basically
a sequential number from 1 to n. Occasionally there are skips between
numbers. I would like to print 'record skipped' for each record
missing from the sequence. For example, if there were no records
numbered 5 and 6 in a sequence...
|
by: jtidwell |
last post by:
I am developing a Work Order Database for my job. I have a combo box with "Contract Numbers" to select from. When you select on any Contract Number I need a new "Work Order Number" to appear. There are 10 different contract numbers so I need 10 different work order numbers (N10001, O100001, etc..) to generate sequential for each new record.
|
by: Finomosec |
last post by:
Hi,
i have a table of number-objects with beginning and endnr:
10-15
16-20
25-30
32-32
35-35
36-36
37-40
|
by: Excel 009 |
last post by:
Hi,
Is there a way to populate sequential numbers in a field using SQL or
VBA? If yes, how?
Assume the following is my existing table:
Fruit ID
Apply
Banana
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |