Find gaps in sequence of numbers
Hi,
I'm trying to scan through multiple records containing millions of numbers. What I need to do is to print out the numbers that are sequential as such:
If the record contains a sequence like this:
46859995600
46859995699
And there is no breakage in that sequence, then print:
46859995600 100
If that sequence is broken somewhere, then print series of 10 that are sequential, f.e:
46859995600 10
46859995620 10
46859995630 10
I already have a bash script for this, but it's way too slow. If anyone can help me out I'd be very grateful, perhaps a "awk" solution.
Thanks!
Indeed. Also, note that the code above makes several assumptions: 1) monotonicity ... but "sort" can take care of that; 2) there is no zero. This would be easy to fix, but in the first draft I didn't bother with it: 000-009 will not currently be counted; 3) the application is not mission critical; 4) there are no blank line; 5) each line contains *only* the number.
On #3, my code may be faster, but it hasn't been checked for accuracy. Be sure to test for accuracy before trusting its output. In fact, I just tested it ... it failed.
I tested it after I recoded it in C. The speed went from 2min 3sec to 0.39sec ... that's nice. But the results file was *much* smaller.
The results from the bash version includes several duplicates, and some erroneous inclusions. Given that so far the C version seems more accurate (but still needs testing) and *much* faster, I'd go with patching this up further:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
unsigned long lt = 0, lh = 0;
unsigned long n, prev = 0, tens[10];
int nten=0;
void check_tens() {
int i;
if (lh) printf("%lu 100\n",lh);
else for (i = 0; i < 10 && tens[i]; i++)
printf("%lu 10\n",tens[i]);
memset(tens,0,10*sizeof(unsigned long));
nten = 0;
int main() {
int i;
memset(tens,0,10*sizeof(unsigned long));
while (fscanf(stdin,"%lu\n",&n) == 1) {
if ((n % 10) == 0) lt = n;
else if ((n % 10) == 9) {
if (!lt || n - 9 == lt) tens[nten++] = lt;
lt = 0;
if ((n % 100) == 0) lh = n;
else if ((n % 100) == 99) check_tens();
if (n - prev != 1) {
lt = lh = 0;
for (i = prev; i < n; i++)
if ((i % 100) == 99) { check_tens(); break; }
if ((n % 10) == 0) lt = n;
if ((n % 100) == 0) lh = n;
prev=n;
lh = 0;
check_tens();
return 0;
EDIT: I can actually confirm the C version is not yet accurate. ARGH!
Last edited by Trilby (2014-05-02 00:11:09)
Similar Messages
-
How can I find unknown number between specific numbers?
How can I find unknown number between specific numbers? For example I want to find the start with value from following question... How can I do that? If you run this oracle query SELECT DBMS_METADATA.GET_DDL('SEQUENCE','SEQUENCE_NAME') FROM DUAL to find out my sequence DDL you will see the similar given values that assigned to sequence DDL, and if I can find the result here I will apply same technique to my C# code to find start with value of sequence in oracle.Thanks –
Given Values
Min number=20
Max Number=500
increment number=10
start with=? needs to be find.
how many times it increments= 5
where the cycle stopped now= 205
Conditions;
1. Start with number has to be either 20 or 500 or between 20-500...
2. It always increment 5 times.For example, If the start with number is 75 than numbers goes like First cycle=(75,85,95,105,115) Second cycle=(125,135,145,155,165)...Third cycle (175,185,195,205) and it stops on 205.
Question is If start with number not known. is there any way to find it?
Thanks!DUPLICATE THREAD!
Please do NOT keep creating duplicate threads.
https://forums.oracle.com/thread/2578906
https://forums.oracle.com/thread/2578830
Your question has already been answered in your other thread. As I said in your other thread:
The START WITH value part of the DDL for a sequence is NOT stored in the data dictionary. See the SYS.SEQ$ table for the attributes stored in the dictionary.
What part of that are you having trouble understanding?
There IS NO START WITH to find - it does NOT exist!
Mark this thread ANSWERED and continue using your 2578830 thread if you intend to pursue this further. -
Finding Gaps within an individual Track
Hi
Is it possible to find gaps in Video Track one if there is a continuous audio track below it?
I have tried selecting the header of video 1 along with selecting the whole track. Then deselecting all other headers.
When i use sequence/ go to gap it doesn't.
Thanks
GaryNot a problem
Note that, by default, these commands don't have keyboard shortcuts. If you're into that sort of thing, open up Edit > Keyboard Customization, and you can set shortcuts for these in Application > Sequence > Go to Gap. -
How to find what planning sequences/functions update data in a cube?
Hi, Experts:
We have a real time cube being updated by planning sequences/functions. How can I find what planning sequences/functions are updating this cube?
Thanks,
JennyHi,
You can go the transaction code: rsa1 and then select "Meta data Repository" option on the left side.
You can use Meta data repository to view the dependent objects for a infocube or any other BW object.
In your case, you can select "Infocube" option on the left side and then go on selecting your infocube.
Now you will get all dependent objects created on this infocube, which will include your plannign functions also.
Refer top the following link for more details on "Meta data Repository",
http://help.sap.com/saphelp_nw04/helpdata/en/4e/ea683c25e9096de10000000a114084/frameset.htm
Regards,
Balajee -
How to find out which sequence a certain column used?
you konw column C in table T is created by a sequence.
How to find out which sequence column C is using?In the meantime I think it ought to becompulsory to
only assign sequence values in triggers.
Why? I can see no good reason for this. I think it's a good idea to have stuff like this
coded in only one placeI don't like your argument for compulsory using triggers for sequences for exactly the same reason as you, ie that I like to keep my code in one place. If I'm reading some pl/sql code which uses a sequence I like to see the fact that the sequence is used in the pl/sql code, rather than to check to see if theres a trigger on the table and then see which sequence that uses.
"naming conventions can work, provided they're enforced. Alas, like other standards, they can only be enforced by active policing".
Thats what code reviews are.
" there is also the special case of one sequence populating more than one table...."
This is My Pet hate. I don't think there's ever a case where this is absolutely necessary. Again code reviews
Unfortunately I have to live with a system which does this.
and I appreciate we can't all have code reviews, etc. -
How can i find out the last 5 numbers dialed without having the phone
how can i find out the last 5 numbers dialed without having the phone
log into your myverizon account,go to detail useage
-
How to find gaps in data?
Hi!
I have the following problem.
Data:
range_id actual_nr
AAA 001AAA
AAA 002AAA
AAA 003AAA
AAA 006AAA
AAA 007AAA
AAA 009AAA
BBB 001BBB
BBB 002BBB
etc.
I have to get report in the following form
from to nr_of_rows
001AAA 003AAA 3
006AAA 007AAA 2
009AAA 1
001BBB 002BBB 2
etc.
As you can see if there is a gap in sequence then I have to calculate how many rows were in sequence before the gap.
Can somebody give me some hints or even working statement?How's this?
WITH
Sample_Data
AS
SELECT 'AAA' range_id, '001AAA' actual_nr FROM Dual UNION ALL
SELECT 'AAA' range_id, '002AAA' actual_nr FROM Dual UNION ALL
SELECT 'AAA' range_id, '003AAA' actual_nr FROM Dual UNION ALL
SELECT 'AAA' range_id, '006AAA' actual_nr FROM Dual UNION ALL
SELECT 'AAA' range_id, '007AAA' actual_nr FROM Dual UNION ALL
SELECT 'AAA' range_id, '009AAA' actual_nr FROM Dual UNION ALL
SELECT 'BBB' range_id, '001BBB' actual_nr FROM Dual UNION ALL
SELECT 'BBB' range_id, '002BBB' actual_nr FROM Dual
SELECT
MIN(actual_nr_start) actual_nr_start,
actual_nr_finish,
MAX(Total)
FROM
SELECT
range_id,
MIN(actual_nr) actual_nr_start,
MAX(actual_nr) actual_nr_finish,
MAX(Level) Total
FROM
Sample_Data
CONNECT BY
range_id = PRIOR range_id
AND substr(actual_nr, 1, 3) = substr(PRIOR actual_nr, 1, 3) + 1
GROUP BY
range_id,
CONNECT_BY_ROOT actual_nr
GROUP BY
actual_nr_finish
ORDER BY
SubStr(actual_nr_start, -3),
actual_nr_start;Message was edited by:
Brian Tkatch 2
consolidated two levels. -
Strange display of a long sequence of numbers - Should I press OK ?
From type to time my iphone displays a long sequence of numbers and waits for me to press a green "OK" button. For example last sequence starts with 1259752976 1 199.664277......
Anybody knows what's going on ?Yolanda,
Yolanda Arroyo wrote:
Hi folks,
I have problems with my Tuxedo cofiguration using domains.
I have Tuxedo8 installed in Linux 7.1. It connects to an Oracle 8.1.7 database,
and has 2 domains cofigured to connect to a remote domain. (this domain is in
fact WebLogic Server 6.1 running in the same machine)
If I run this configuration, the servers in LDOM2 (for example, server1) are not
able to connect to the database (this group don't include a TMS because is
associated with the domain, and I cannot put an OPENINFO information in that
group, for the same reason)
BUT!!!! the most amazing thing is that, if I make no changes in the bdmconfig
file, and in the UBBCONFIG I change the group WTC2 for a group that contains a
TMS, in this way:
I think that you are confusing the purpose of the 2 config files, ubbconfig and
dmconfig.
You should configure you application servers in the ubbconfig file. They
should be in server groups configured with the correct TMS servers, OPENINFO, etc.
One you have done this, you have a single domain.
To extend this to talk to external domains, now you need to configure the gateways.
Add a new server group to your ubbconfig and configure the domain gateway
processes (GWADM, DMADM, GWTDOMAIN) in it. This group does not need a TMS (TMS
services are provided by the domain gateway) or any OPENINFO.
Now, write a dmconfig file. This does not reference any servers it does
reference services that you want to import from other domains, or export to them.
I hope this explains how you can successfully get your configuration working...
Regards,
Peter. -
Cryptic sequence of numbers as sender name for invitations
When I want to send an calendar invitation to my business address, my sender name is a cryptic sequence of numbers instead of "first name last name". These are the steps:
1. Receiving Email form my iPad with the invitation
2. Download the attached ics to my desktop of the windows PC
3. Drag&Drop the ics to the Lotus Notes Calendar
4. The invitation is visible, the sender name is i.e. 2fjfu3oodsjdf90499fj993efj0ßßejfefjß39eß939hgsdlfnasdäfa9e9
I know that this invitation is from my private account. But what about friends I wanted to invite? Or should I invite only friends with apple equipment?!?! ;-)Well,
Nokia MfE doesn't reply to my mails. I guess that only poeple using MfE v 2.09.158 who are not registered to the MfE Service (I guess you have to pay a monthly fee as it is with the Blackberry service) have the sender name problem. Of course you can use MfE with other mail providers than Nokia MfE. But in this case "Mail for Exchange" will appear as sender name.
I now switched over to MfE's competitor "RS" and don't have this annoying problem anymore. The alternative to RS would be using the old MfE version where I didn't have that sender name prob. But as my battery was empty after 15 hrs using the old MfE version (with RS my battery lasts 3 days !!!) there is no real alternative to RS!
This sender name problem seems to be part of Nokia's business strategy. As they want to compete with Blackberry they want you to register (and pay the MfE mail service monthly). Probably that's the reason why you get MfE for free (RS costs 50 US$).
Any comments on my theory?!
BR, Johnny -
Function to find the greatest of two numbers
Hi All
In the rtf template, is there any function to find the greatest between two numbers?
Please help
Thanks in AdvanceJust like SQL, use the GREATEST function. List of comparable SQL functions is shown on or about page 9-30 of user guide (depends on which version you have). Search on GREATEST and you'll find it.
-
Find gap between two dates from table
Hello All,
I want to find gap between two dates ,if there is no gap between two dates then it should return min(eff_dt) and max(end_dt) value
suppose below data in my item table
item_id eff_dt end_dt
10 20-jun-2012 25-jun-2012
10 26-jun-2012 28-jun-2012 There is no gap between two rows for item 10 then it should return rows like
item_id eff_dt end_dt
10 20-jun-2012 28-jun-2012
item_id eff_dt end_dt
12 20-jun-2012 25-jun-2012
12 27-jun-2012 28-jun-2012 There is gap between two rows for item 12 then it should return like
item_id eff_dt end_dt
12 20-jun-2012 25-jun-2012
12 27-jun-2012 28-jun-2012
I hv tried using below query but it giv null value for last row
SELECT item_id, eff_dt, end_dt, end_dt + 1 AS newd,
LEAD (eff_dt) OVER (PARTITION BY ctry_code, co_code, item_id ORDER BY ctry_code,
co_code, item_id) AS LEAD,
(CASE
WHEN (end_dt + 1) =
LEAD (eff_dt) OVER (PARTITION BY ctry_code, co_code, item_id ORDER BY ctry_code,
co_code, item_id, eff_dt)
THEN '1'
ELSE '2'
END
) AS new_num
FROM item
WHERE TRIM (item_id) = '802'
ORDER BY ctry_code, co_code, item_id, eff_dtI m using oracle 10g.
please any help is appreciate.
Thanks.Use start of group method:
with sample_table as (
select 10 item_id,date '2012-6-20' start_dt,date '2012-6-25' end_dt from dual union all
select 10,date '2012-6-26',date '2012-6-26' from dual
select item_id,
min(start_dt) start_dt,
max(end_dt) end_dt
from (
select item_id,
start_dt,
end_dt,
sum(start_of_group) over(partition by item_id order by start_dt) grp
from (
select item_id,
start_dt,
end_dt,
case lag(end_dt) over(partition by item_id order by start_dt)
when start_dt - 1 then 0
else 1
end start_of_group
from sample_table
group by item_id,
grp
order by item_id,
grp
ITEM_ID START_DT END_DT
10 20-JUN-12 26-JUN-12
SQL> SY. -
How to remove gap in sequence?
Hi,
SELECT MENU_ITEMID.NEXTVAL INTO :MENU_ITEM.M_ITEM_ID
FROM SYS.DUAL;
EXCEPTION
WHEN OTHERS THEN MESSAGE ('FAILED');
RAISE FORM_TRIGGER_FAILURE;
I have applied the above PRE-INSERT trigger on my MENU_ITEM FORM.
I was inserting values but when I was inserting the 53th item my PC was struck by a power failure, I did not commit or Rollback and next item when I rebooted my system and tried to insert the 53th item in my form it gave me a gap and jumped to 54th item directly.
Now How can I remove this gap?
I tried to insert the ids manually using INSERT command,
INSERT INTO MENU_ITEM VALUES (53, 'AAAA');
INSERT INTO MENU_ITEM VALUES (54, 'BBAAA');
INSERT INTO MENU_ITEM VALUES (55, 'UUAA');
but when I opened the application form then it showed the wrong order of id column.
like
53
55
54
and so on.
I deleted the newly inserted records using this command, DELETE from MENU_ITEM where M_ITEM_ID in(53,54,55);
COMMIT;
and then i queried the nextval of sequence.
and it was 55.
now I want tthe solution to solve this.
Please Help me that where I can find the solution?
Thanks In advance
NataliaI'm afraid you do not understand how a SEQUENCE works.
A sequence is a "one-time" sheet. Once we have got a number through NEXTVAL the sequence can never give us that number again (unless we created the sequence with the CYCLE clause, in which case we'll see it again, eventually). NEXTVAL is a piece of DDL, so we cannot roll it back. We can only fill in the gaps manually, as you did. You, however, overshot and inserted a row with an ID 55 that was in advance of the last number assigned by the sequence.
Also note that the SEQUENCE number has a CACHE; this is the default behaviour, because it is memory efficient. As a side effect, if the database goes down, assigned numbers in the cache may be lost. We can avoid this with the NOCACHE option, but it makes sequence usage slower. It depends: do we need every number in the range or can we cope with gaps? As most sequence usage is simply to assign uniqueness gaps do not matter. They do matter in some circumstances, e.g. in an audit subsystem.
The LAST_NUMBER column on USER_SEQUENCES shows the last number assigned. But, as the example below shows, beware. Before we have called the sequence in this session, LAST_NUMBER shows the actual last number given by the sequence. Once the sequence has satisfied a NEXTVAL call the LAST_NUMBER column shows the last number in the current cache. We can see the actual last number assigned with a CURRVAL statement.
Regards, APC
P.S. The rows in your block are being displayed in the "wrong" order because your block does not have an ORDER BY property to sort rows by ID.
SQL> CREATE SEQUENCE test_seq;
Sequence created.
SQL> SELECT last_number, cache_size
2 FROM user_sequences
3 WHERE sequence_name = 'TEST_SEQ';
LAST_NUMBER CACHE_SIZE
1 20
SQL> SELECT test_seq.NEXTVAL FROM dual;
NEXTVAL
1
SQL> SELECT last_number, cache_size
2 FROM user_sequences
3 WHERE sequence_name = 'TEST_SEQ';
LAST_NUMBER CACHE_SIZE
21 20
SQL> SELECT test_seq.NEXTVAL FROM dual;
NEXTVAL
2
SQL> SELECT last_number, cache_size
2 FROM user_sequences
3 WHERE sequence_name = 'TEST_SEQ';
LAST_NUMBER CACHE_SIZE
21 20
SQL> SELECT test_seq.NEXTVAL FROM dual;
NEXTVAL
3
SQL> ROOLBACK;
Rollback complete.
SQL> SELECT last_number, cache_size
2 FROM user_sequences
3 WHERE sequence_name = 'TEST_SEQ';
LAST_NUMBER CACHE_SIZE
21 20
SQL> SELECT test_seq.NEXTVAL FROM dual;
NEXTVAL
4
SQL> SELECT last_number, cache_size
2 FROM user_sequences
3 WHERE sequence_name = 'TEST_SEQ';
LAST_NUMBER CACHE_SIZE
21 20
SQL> SELECT test_seq.CURRVAL FROM dual;
NEXTVAL
4
SQL> -
Hi Guys
When i execute transaction F-28, i got the document number as 1080000669
But when i go to FB03 and check this document number this number is missing, but all other numbers are available
SAP has given a report to check this missing document number, the transaction code is S_ALR_87012342
When i executed this report, i got the missing document number 1080000669
Is it possible to analyse and find the reason for the same by any chance
Thanks
BalaHi,
Gaps in document number range are possible; read note 175047 for comprehensive information.
Regards,
Eli -
Hi,
I created a sequence and a trigger for autonumber, as a primary key in my CAND_PERSONAL_INFO table. Every time i insert a row in the table, i get long gaps between my autonumber. I don't understand why cant i get a series of numbers without any gaps in my autonumber column.Sequence is not a good way of automatically generating the numbers due this gap problem.I disagree. Sequences are the best for auto-generating unique numbers. Why are gaps a problem?
The second solution is to fire a query select max(num) and add 1 to it every time you want to generate a number. That is a horrible idea in a multi-user environment. This will fail when 2 users do this at the time. -
Trouble with SEQUENCE, loosing numbers
Hi all, I use a sequence to generate the number of an invoice like this:
Insert into table
values(owner.sequence_name.nextval);
But, if an error occurs this number get lost because of the way the sequence works, how can I avoid this??, there is another way to do it??? It doesnt matter if that solution doesn't use the sequence, but my design cannot accept numbers like 1,3,4,5,7, it has to be 1,2,3,4,5,6,7....
Leo
nullUnfortunately, this is another one of those concepts that sounds simple but isn't.
Yours is not an Oracle (or any other DB) problem; it is a design problem.
The reason that none of the suggested alternatives solve your problem is that there are undefined elements in your requirements.
There are actually two aspects to your of problem: 1) generating unique sequential numbers and 2) using those numbers.
I would suggest starting over and try to clearly define the problem and the requirements.
For example your stated requirements appear to be something like:
1. Invoice numbers must to be unique
2. Invoice numbers must be numeric
3. The first invoice number must be 27 (modify per your requirements)
4. Each new invoice must be numbered one higher than the previous invoice.
5. Invoice numbers must be sequential with no missing numbers.
While the above list seems to be complete there are some very important questions that are not yet answered.
1. When does an invoice become an invoice? That is, when is the invoice number assigned? Is the number assigned before or after the invoice record is committed to the database? If the number is assigned before committing (e.g. when the user begins processing an invoice) but the invoice is then abandoned and never committed there will be a gap in invoice numbers.
2. Can invoice records ever be deleted? If a committed record is deleted there will be a gap in the numbers. Some systems handle deletions by either flagging the records as deleted without actually deleting them or moving the records to a deleted table so that all invoice numbers are always accounted for.
Check printing applications used to print on pre-numbered check forms. The line printers that were used had sprockets to feed the checks and the first check could not be printed on since the sprockets were above the print head. The operator would enter the first check number into the system and the application created a check record marked VOID for that check number. This ensured that all pre-printed checks were accounted for and had not been misplaced or stolen.
Oracle's sequence generator can generate sequence numbers in sequential order and ensure that there are no gaps in the generation process.
Oracle cannot ensure that your application uses those numbers in a way that avoids gaps.
Even a normal INSERT trigger that might be used to get the next sequence value cannot be certain that the record it is inserting will actually be committed unless the COMMIT operation is performed within the trigger.
Sorry to 'rain on your parade' but withouth a better problem definition we can't give you a better solution.
Maybe you are looking for
-
Can no longer print to shared printer on mac running 10.5
We have an old G5 that we use to share a printer between various macs all running 10.9. I recently upgraded the G5 to 10.5 to see if that helped with some issues we had, but now that we can no longer print to the printer. What I have done so far: #1
-
E-Recruiting: CP-S relationship missing?
Hi all, I'm working on E-Recruiting 6 and in the IMG documentation I read in the section "BAdI: Inbound Processing HR Master Data" that "The Central Person (object type CP) is assigned to the position instead of the Person (object type P)". Also in t
-
Balance interest calculation on vendors
Hi, Is it possible to calculate interest on vendor balances? I did all the settings for interest calculation and assigned the interest indicator to the vendor master. When I run the interest calculation program (Free Selection) a batch input session
-
Create Purchase Order - Account Assignment Category Q
Hi experts, I'm trying to create a purchase order with account assignment category "Q" and the system show me the error message ME083 - Enter GR non-valuated. The material type is HIBE. The Quantity and value updating are flagged. Does anybody can he
-
Hi all truble in goods movement
hi all i want to create a new goodsmovement using bapi FM. for that i use "bapi_goodsmvt_create ". problem is thats when i am creating the goods movement by using txn mb11 it works and when the same data i pass to the fm in se37 it generates a prob