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;
3 5085
Here is my PL/SQL-solution for the problem.
But i would still like to see your SQL-solutions (if any). - CREATE OR REPLACE TYPE beg_end AS OBJECT (beginning VARCHAR2(15), endnr varchar(15));
-
/
-
-
CREATE OR REPLACE TYPE beg_end_set IS TABLE OF beg_end;
-
/
-
-
CREATE OR REPLACE FUNCTION groupNumbers RETURN beg_end_set PIPELINED IS
-
CURSOR pns IS SELECT * FROM numbers ORDER BY beginning ASC;
-
res beg_end := beg_end(null, null);
-
curPN numbers%ROWTYPE;
-
beginning VARCHAR2(15) := null;
-
endnr VARCHAR2(15) := null;
-
BEGIN
-
OPEN pns;
-
LOOP
-
FETCH pns INTO curPN;
-
EXIT WHEN pns%NOTFOUND;
-
IF beginning IS NULL THEN
-
beginning := curPN.beginning;
-
ELSIF to_number(endnr) + 1 != to_number(curPN.beginning) THEN
-
res.beginning := beginning;
-
res.endnr := endnr;
-
PIPE row(res);
-
beginning := curPN.beginning;
-
END IF;
-
endnr := curPN.beginning;
-
END LOOP;
-
IF beginning IS NOT NULL THEN
-
res.beginning := beginning;
-
res.endnr := endnr;
-
PIPE row(res);
-
END IF;
-
CLOSE pns;
-
return;
-
END;
-
/
-
SHOW ERRORS
-
-
-
-- then it can be used like this ...
-
select count(*) from table(groupNumbers);
-
-
-- count for diff ...
-
select count(*) from numbers;
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: -
SELECT min(beginning), max(endnr)
-
FROM numbers n
-
group by where (prev.endnr +1 = n.beginning);
-
Maybe its just another syntax to use ...
Any ideas?
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: - select
-
min(beginning) beginning, -- #4
-
max(endnr) endnr, -- #4
-
row_number() over (order by grp),
-
grp,
-
count(*)
-
from (
-
select
-
s.*,
-
SUM(diff) OVER (order by beginning) grp -- #2
-
from (
-
select
-
beginning,
-
endnr,
-
beginning - NVL(LAG(endnr + 1) OVER (ORDER BY beginning asc), beginning) diff -- #1
-
FROM numbers
-
) s
-
)
-
group by grp -- #3
-
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;
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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: 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
|
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: 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...
| |
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: 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!
|
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,
|
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...
|
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: 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: 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...
|
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: 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();...
|
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...
| |