473,614 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Grouping sequential numbers with SQL

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

And what i need is the min(beginning) and max(endnr) of each group containing sequential objects with PREV.endnr+1=NE XT.beginning:
10-20
25-30
32-32
35-40

I found SQL Query - Find block of sequential numbers, but it seems to be just the negation of what i need in a way that is not reversed so simply (or i did not really understand what happened there).

I thought about using hierarchical queries, but could not it out.

A workaround to the problem would be writing a PL/SQL-Function, but i dislike that. Pure SQL would be better.

Greetings Finomosec;
Jul 6 '07 #1
3 5085
Finomosec
7 New Member
Here is my PL/SQL-solution for the problem.
But i would still like to see your SQL-solutions (if any).

Expand|Select|Wrap|Line Numbers
  1. CREATE OR REPLACE TYPE beg_end AS OBJECT (beginning VARCHAR2(15), endnr varchar(15));
  2. /
  3.  
  4. CREATE OR REPLACE TYPE beg_end_set IS TABLE OF beg_end;
  5. /
  6.  
  7. CREATE OR REPLACE FUNCTION groupNumbers RETURN beg_end_set PIPELINED IS
  8.     CURSOR pns IS SELECT * FROM numbers ORDER BY beginning ASC;
  9.     res beg_end := beg_end(null, null);
  10.     curPN numbers%ROWTYPE;
  11.     beginning VARCHAR2(15) := null;
  12.     endnr VARCHAR2(15) := null;
  13. BEGIN
  14.     OPEN pns;
  15.     LOOP
  16.         FETCH pns INTO curPN;
  17.         EXIT WHEN pns%NOTFOUND;
  18.         IF beginning IS NULL THEN
  19.             beginning := curPN.beginning;
  20.         ELSIF to_number(endnr) + 1 != to_number(curPN.beginning) THEN
  21.             res.beginning := beginning;
  22.             res.endnr := endnr;
  23.             PIPE row(res);
  24.             beginning := curPN.beginning;
  25.         END IF;
  26.         endnr := curPN.beginning;
  27.     END LOOP;
  28.     IF beginning IS NOT NULL THEN
  29.         res.beginning := beginning;
  30.         res.endnr := endnr;
  31.         PIPE row(res);
  32.     END IF;
  33.     CLOSE pns;
  34.     return;
  35. END;
  36. /
  37. SHOW ERRORS
  38.  
  39.  
  40. -- then it can be used like this ...
  41. select count(*) from table(groupNumbers);
  42.  
  43. -- count for diff ...
  44. select count(*) from numbers;
Jul 6 '07 #2
Finomosec
7 New Member
I tried to figure out a way to solve the problem using Oracle's analytic functions, but did not find a way to solve the problem so far.

I need a funtion to group rows together by a where-clause.
Something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT min(beginning), max(endnr)
  2. FROM numbers n
  3. group by where (prev.endnr +1 = n.beginning);
  4.  
Maybe its just another syntax to use ...

Any ideas?
Jul 9 '07 #3
Finomosec
7 New Member
I think i found a solution. The only thing is ... its result differs from the PL/SQL-function above. I have to look into this again.

But anyway ... here is my solution to group sequential numbers with SQL:

Expand|Select|Wrap|Line Numbers
  1. select
  2.     min(beginning) beginning, -- #4
  3.     max(endnr) endnr, -- #4
  4.     row_number() over (order by grp),
  5.     grp,
  6.     count(*)
  7. from (
  8.     select
  9.         s.*,
  10.         SUM(diff) OVER (order by beginning) grp -- #2
  11.     from (
  12.         select
  13.             beginning,
  14.             endnr,
  15.             beginning - NVL(LAG(endnr + 1) OVER (ORDER BY beginning asc), beginning) diff -- #1
  16.         FROM numbers
  17.     ) s
  18. )
  19. group by grp -- #3
  20. order by grp asc;
1. calculate the difference between the previous endnr +1 and the current beginning (this is 0 if they are consecutive)
2. in a surrounding query calculate the sum of all previous diff-values (this is number increasing on non-consecutive numbers and staying the same on consecutive ones)
3. group by the calculated sum (grp)
4. get min/max or whatever needed from the group

Warning: The beginning and endnr have to be numerical!! (use TO_NUMBER(varch ar_column) if needed).

The values "row_number () over (order by grp)", "grp" and "count(*)" are only for demonstration purpose and can be removed afterwards.

Greetings Finomosec;
Jul 9 '07 #4

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

Similar topics

6
8077
by: cjm | last post by:
I need to group records and assign a setid to the group. I have a table with data that looks like this ColA ColB 94015 01065 94016 01065 94015 01085 94015 01086 33383 00912 32601 00912
6
12699
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
5
3204
by: Lapchien | last post by:
I have list of numbers in a table (originally from autonumber in a different database) from 1 to 1,000,000. The list is not in sequential order - there are loads of numbers missing. How can I identify what numbers are missing? Thanks, Lap (I'd like to then use this 'missing number list' to use for new records, instead of autonumber - I think I need to use DMax - can someone summarise
14
12027
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.
1
2447
maxamis4
by: maxamis4 | last post by:
Hello folks, I have two forms a parent form and a subform. The parent form is an unbound form while the subform is a form that contains all a list of what I like to call 'in stock ' phone numbers. with in that subform the user has the ability to select between to radio buttons to do a bulk selection. The first option lets the user select the first X amount of numbers that he or she wants. The second option selects X amount of numbers but...
6
6964
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.
11
1655
by: clarencelai | last post by:
Hi.. I have a set of numbers that are not sequential. For example, 1, 2, 3, 4, 6,7,8,9,10,14,15. I have tried to use the FIRST/LAST and MIN/MAX in QUERY but it didn't work. It will return 1 and 15. How do I get results returned as (1,4), (6,10) and (14,15)? VB codes or SQL would be great!
0
1524
by: Roman Bertle | last post by:
Hello, I try to format monetary values using the locale module, python2.5: Python 2.5.2a0 (r251:54863, Jan 3 2008, 17:59:56) on linux2 Type "help", "copyright", "credits" or "license" for more information. 'de_AT.utf8' {'mon_decimal_point': ',', 'int_frac_digits': 2, 'p_sep_by_space': 1, 'frac_digits': 2, 'thousands_sep': '', 'n_sign_posn': 1,
9
2934
by: Axxe | last post by:
I have searched high and low for cogent, well-explained coding to complete a project on which I have spent six months of work. I stumbled across something on this site that is close to what I seek, but, as is rather typical, I could understand neither the question nor the answer. I am hopeful that someone in this forum can provide me with suitable guidance. THE ISSUE I am working in Access 2003. The database I am preparing...
0
8120
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
8620
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...
1
8265
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,...
0
7047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6085
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
5537
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2560
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
1
1705
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1420
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.