473,769 Members | 7,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Grouping Sequential Numbers with like attributes

8 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. SELECT *
  2. FROM (SELECT first_consecutive = MIN(part_number), last_consecutive,
  3. length = last_consecutive - MIN(part_number) + 1
  4. FROM (SELECT P1.part_number,
  5. MIN(P2.part_number) AS last_consecutive
  6. FROM PartNumbersAvailable AS P1
  7. LEFT JOIN (SELECT P1.part_number
  8. FROM PartNumbersAvailable AS P1
  9. LEFT JOIN PartNumbersAvailable AS P2
  10. ON P2.part_number = P1.part_number + 1
  11. WHERE P2.part_number IS NULL) AS P2
  12. ON P2.part_number >= P1.part_number
  13. GROUP BY P1.part_number) AS P
  14. GROUP BY last_consecutive) AS P
  15. WHERE length >= 5
  16. ORDER BY first_consecutive
Any help would be greatly appreciated!

Thanks!
Aug 17 '10 #1
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
Aug 17 '10 #2
Joel Miller
8 New Member
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!
Aug 17 '10 #3
ck9663
2,878 Recognized Expert Specialist
What would happen if there's a gap on the sequence but they have the same type?

~~ CK
Aug 17 '10 #4
Jerry Winston
145 Recognized Expert New Member
This is an interesting requirement, I'll see if I can help.
Aug 17 '10 #5
Joel Miller
8 New Member
That would be a new record. Only sequential phone numbers with exactly the same type, description, and LDCarrier would be grouped.

Thanks!
Aug 17 '10 #6
Joel Miller
8 New Member
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!
Aug 17 '10 #7
Jerry Winston
145 Recognized Expert New Member
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:

Expand|Select|Wrap|Line Numbers
  1. DECLARE @tbl TABLE(number Bigint,[type] char(10),[description] char(20),Carrier char(10))
  2. DECLARE @t2 TABLE(number Bigint,[type] char(10),[description] char(20),nSet int,Carrier char(10))
  3. DECLARE @c BIGINT
  4. DECLARE @nSet INT 
  5. DECLARE @pNum BIGINT 
  6. DECLARE @num bigint
  7. DECLARE @cName char(10)
  8. Declare @desc char(10)
  9. DECLARE @type char(10)
  10.  
  11. SET @pNum = 0
  12. SET @nSet = 0
  13.  
  14. SET @c = 3098699700
  15. WHILE @c <= 3098699776
  16. BEGIN
  17.     INSERT INTO @tbl VALUES(@c,'East','My Descript','Carrier A');
  18.     SET @c = @c + 1
  19. END
  20.  
  21. SET @c = 3098699777
  22. WHILE @c < =3098699777
  23. BEGIN
  24.     INSERT INTO @tbl VALUES(@c,'West','My Descript','Carrier B')
  25.     SET @c = @c + 1
  26. END
  27.  
  28. SET @c = 3098299777
  29. WHILE @c < =3098299777
  30. BEGIN
  31.     INSERT INTO @tbl VALUES(@c,'South','My Descript','Carrier B')
  32.     SET @c = @c + 1
  33. END
  34.  
  35. SET @c = 3098699778
  36. WHILE @c <=  3098699799
  37. BEGIN
  38.     INSERT INTO @tbl VALUES(@c,'North','My Descript','Carrier C')
  39.     SET @c = @c + 1
  40. END
  41.  
  42. DECLARE cr CURSOR FOR
  43. SELECT * FROM @tbl ORDER BY [number]
  44.  
  45. OPEN cr
  46.  
  47. FETCH NEXT FROM cr
  48. INTO @num,@type,@desc, @cName
  49.  
  50.  
  51. WHILE @@FETCH_STATUS = 0
  52. BEGIN
  53.  
  54.     if NOT @num = @pNum + 1
  55.     BEGIN
  56.         print cast(@num as varchar(12)) + ' - ' + cast(@pnum as varchar(12))
  57.         SET @nSet = @nSet + 1
  58.     END
  59.  
  60.     INSERT INTO @t2 (number,[type],[description],nSet,Carrier) VALUES (@num,@type,@desc,@nSet,@cName)
  61.  
  62.     SET @pNum = @num
  63.  
  64.     FETCH NEXt FROM cr
  65.     INTO @num,@type,@desc, @cName
  66. END
  67.  
  68. CLOSE cr
  69. DEALLOCATE cr
  70.  
  71.  
  72. SELECT min(number),max(number),count(*),Carrier
  73. FROM @t2
  74. GROUP BY nSet,[type],[description],Carrier
Aug 17 '10 #8
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
Aug 17 '10 #9
Joel Miller
8 New Member
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!
Aug 17 '10 #10

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

Similar topics

6
12711
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
2
2624
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.
2
3404
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...
14
12048
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.
2
1767
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...
2
5119
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
4
2093
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...
6
6969
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.
3
5093
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
3
4693
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
0
9423
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,...
0
10214
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, 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...
0
10048
jinu1996
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...
1
9996
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,...
1
7410
isladogs
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...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3963
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
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
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...

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.