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.
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
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
}
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