473,486 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create 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=NEXT.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 5068
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(varchar_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
8054
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
12684
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...
5
3192
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...
14
11993
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. ...
1
2442
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...
6
6953
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...
11
1638
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...
0
1521
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...
9
2929
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...
0
7100
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
6964
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
7126
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,...
0
5434
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,...
1
4865
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...
0
4559
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...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
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 ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.