Results 1 to 4 of 4

Thread: SQL Query for Searching Missing Numbers from Sequence of Numbers

  1. #1
    Join Date
    Jan 2009
    Posts
    48

    SQL Query for Searching Missing Numbers from Sequence of Numbers

    Hi there.

    I am trying for a query which helps in finding the first missing number within the sequence of number in a database. Consider the example that , I have a set of fields which may are having the numbers from 1-10 and number missing 11, have the numbers from 12-15, and missing 16, and similar kind.

    I want to perform through the table and explain that 11 as a number is the first number missing out of the sequence of number. This is regarding for an issue of employee numbers in standard order, but in the past there were problems at random so I am trying to clear it out and issue all the missing numbers. So when each time I will run the query, it will find the next lowest missing number.

    Please help me with this.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: SQL Query for Searching Missing Numbers from Sequence of Numbers

    Check this query:

    Select Top 1 YourNumber + 1 from YourTable T1
    where not exists
    (select * from YourTable T2 where T2.YourNumber = T1.YourNumber + 1)
    order by YourNumber asc

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: SQL Query for Searching Missing Numbers from Sequence of Numbers

    var calc = from r in Daily
    join pa in Sparse on
    r.deptno equals pa.deptno
    where pa.date.Equals(
    (from maxpa in Sparse
    where maxpa.date <= r.date
    && pa.deptno == maxpa.deptno
    select maxpa.date ).()Max()
    )
    select new { r.date, r.deptno, r.name, r.data1, r.data2
    //, v =

    +
    // rand.Next().ToString()
    //,
    // Allocation = pa
    }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: SQL Query for Searching Missing Numbers from Sequence of Numbers

    Try this one:

    SQL> insert into ORDERS_TABLE (…, ORDER_NUMBER, …)
    values (…, (select max(ORDER_NUMBER)
    from ORDERS_TABLE) + 1,…);

    SQL> create trigger ORDER_TABLE_SEQ
    after insert on ORDERS_TABLE(update ORDERS_TABLE set ORDER_NUMBER =
    (select count(*) from ORDERS_TABLE)where DBKEY = ORDERS_TABLE.DBKEY
    ) for each row;

    SQL> create trigger SEQUENCE_NUM_TRIG
    after insert on ORDERS_TABLE
    (update SEQ_TABLE set SEQ_TABLE.NUMBER = (select count(*) from ORDERS_TABLE) + 1)
    for each row;

    SQL> create sequence DEPT_ID;

    SQL> create sequence COST_CENTER
    minvalue 100
    maxvalue 999;

    SQL> create sequence CUSTOMER_ID_SEQ
    minvalue integer
    maxvalue integer;
    show sequence customer_id_seq
    CUSTOMER_ID_SEQ
    Sequence Id: 2

    SQL> create sequence COST_CENTER
    start with 245
    minvalue 100
    maxvalue 999;

    SQL> SET FLAGS 'SEQ_CACHE(10000)';

    $ define/user_mode rdms$set_flags “SEQ_CACHE(10000)”
    $ rmu/load/commit_every=600 mydatabase mytable bulkload.dat

Similar Threads

  1. How to change negative numbers to positive numbers in Excel
    By Shaina Na in forum Microsoft Project
    Replies: 3
    Last Post: 08-01-2012, 05:35 PM
  2. Need C program code to count numbers of negative & positive numbers
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 02:00 PM
  3. Random Numbers in C
    By Rob Dizzle in forum Software Development
    Replies: 5
    Last Post: 30-12-2009, 10:37 AM
  4. How to Use Chapter Numbers with Page Numbers In Ms Word
    By Braze in forum Windows Software
    Replies: 3
    Last Post: 27-12-2008, 07:01 PM
  5. Random numbers. Help
    By tetilda in forum Software Development
    Replies: 2
    Last Post: 15-09-2008, 05:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,022,883.41599 seconds with 17 queries