|
|
|
|
Re: Finding missing sequences [message #227322 is a reply to message #227298] |
Wed, 28 March 2007 00:35 |
asherisfine
Messages: 63 Registered: June 2006 Location: India
|
Member |
|
|
Hi
jeteshmnair
The following is the table structure i used
create table asher( num varchar(15));
insert into asher values('26SI6D0001');
insert into asher values('26SI6D0002');
insert into asher values('26SI6D0003');
insert into asher values('26SI6D0005');
insert into asher values('26SI6D0006');
insert into asher values('26SI6D0007');
insert into asher values('26SI6D0008');
insert into asher values('26SI6D0010');
now the follwowing code will give you the values that are missing
with tab as
(select to_number(replace(num,'26SI6D')) su from asher)
select fs from (select '26SI6D'||to_char(lpad(rownum,4,0)) fs from dba_tables where rownum<=(select max(su) from tab))
where fs not in (select num from asher)
SQL> ed
Wrote file afiedt.buf
1 with tab as
2 (select to_number(replace(num,'26SI6D')) su from asher)
3 select fs from (select '26SI6D'||to_char(lpad(rownum,4,0)) fs from dba_tabl
es where rownum<=(select max(su) from tab))
4* where fs not in (select num from asher)
SQL> /
FS
----------
26SI6D0004
26SI6D0009
i used db_tables because i dont have any other table which has rows more than table "asher",
so only dba_table will generate enough rownums.
[Updated on: Wed, 28 March 2007 00:36] Report message to a moderator
|
|
|
|
|
|
Re: Finding missing sequences [message #227337 is a reply to message #227322] |
Wed, 28 March 2007 01:16 |
pavuluri
Messages: 247 Registered: January 2007
|
Senior Member |
|
|
see this query
select '26SI6D'||lpad(su,4,0) from (
(select level as su from dual connect by level<=10
minus
select to_number(replace(num,'26SI6D')) su from asher))
Thanks
srinivas
|
|
|
|
|
|
|
|
|
Re: Finding missing sequences [message #227392 is a reply to message #227387] |
Wed, 28 March 2007 02:32 |
pavuluri
Messages: 247 Registered: January 2007
|
Senior Member |
|
|
Hi All
see this its prefect query
select * from (select fs from (select '26SI6D'||lpad(rownum,4,0) as fs from dual connect by level<=100000)
where fs<=(select max(num) from srinu11)) where fs not in (select num from srinu11)
minus
select * from (select fs from (select '26SI6D'||lpad(rownum,4,0) as fs from dual connect by level<=100000)
where fs<=(select min(num) from srinu11)) where fs not in (select num from srinu11)
thanks
srinivas
|
|
|
|
|
|
|
|
|
|
Re: Finding missing sequences [message #227419 is a reply to message #227298] |
Wed, 28 March 2007 03:42 |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
"Michel Cadot wrote on Wed, 28 March 2007 09:39" |
Did you try it?
I hope you are not in a hurry and have a lot of memory.
"connect by level <= 100000" Ouch!
|
At least it is not necessary when you take only last four digits from the number. 9999 as a limit should be enough.
Personally I would use TO_CHAR with proper format instead of LPAD on numeric value (and rely on implicit conversion). However, if it works...
[Edit: Added this paragraph]
select '26SI6D'||to_char(lpad(rownum,4,0)) fs from <...>
If I remember well, after a standard conversion to varchar, the number contains heading space. The column would contain values '26SI6D00 1', '26SI6D00 2', ..., '26SI6D9999', so it would not 'work'. With the enhanced limit, the sequence will continue with '26SI6D0000', '26SI6D0001' and so on, so it would start 'working'.
On the other hand, if using
select '26SI6D'||to_char(rownum,'fm0999') fs from <...>
(which I would prefer) the limit shall not exceed 9999.
[Updated on: Wed, 28 March 2007 04:12] Report message to a moderator
|
|
|
|
Re: Finding missing sequences [message #227508 is a reply to message #227322] |
Wed, 28 March 2007 07:39 |
|
ebrian
Messages: 2794 Registered: April 2006
|
Senior Member |
|
|
asherisfine wrote on Wed, 28 March 2007 01:35 |
1 with tab as
2 (select to_number(replace(num,'26SI6D')) su from asher)
3 select fs from (select '26SI6D'||to_char(lpad(rownum,4,0)) fs from dba_tabl
es where rownum<=(select max(su) from tab))
4* where fs not in (select num from asher)
|
This assumes the invoice_id sequence number starts at 0001 and if it doesn't start at 0001, the query will actually return records back from 0001 to the lowest invoice_id in addition to the filling in the gap values.
A slightly more performant query that doesn't rely on a dictionary table and only fills in the gap between the range of low & high invoice_id:
select * from (
select '26SI6D'||to_char(mn + level -1, 'fm0999') val
from (
select min(replace(invoice_id, '26SI6D')) mn
, (max(replace(invoice_id, '26SI6D')) - min(replace(invoice_id, '26SI6D'))) btwn
from inv)
connect by level <= btwn + 1)
where val not in (select invoice_id from inv);
I agree with flyboy's use of TO_CHAR, however I believe it is a moot point in this case.
|
|
|
|
Re: Finding missing sequences [message #227534 is a reply to message #227531] |
Wed, 28 March 2007 08:53 |
pavuluri
Messages: 247 Registered: January 2007
|
Senior Member |
|
|
see this query is working
but it is complex.
select * from (select fs from (select '26SI6D'||to_char((rownum+1-1),'fm009999') as fs
from dual connect by level<=100000)
where fs<=(select max(num) from srinu111)) where fs not in (select num from srinu111)
minus
select * from (select fs from (select '26SI6D'||to_char((rownum+1-1),'fm009999') as fs
from dual connect by level<=100000)
where fs<=(select min(num) from srinu111)) where fs not in (select num from srinu111)
|
|
|
|
|
Re: Finding missing sequences [message #227789 is a reply to message #227711] |
Thu, 29 March 2007 07:25 |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
Suppose, it is compound from prefix '26SI' plus '6' for the year's last digit (what about 2010 and after?) plus letter for month (from January 'A' to December 'L') plus three digits (001 to 999). Then simple extraction of these values from input date solves it:
SELECT '26SI' -- prefix
||substr(to_char(input_date, 'YYYY'), 4, 1) -- year
||chr(to_number(to_char(input_date, 'MM'))+64) -- month
||to_char(mn + level -1, 'fm099') val
This will work only for one month, so only one input date from that month is sufficient.
|
|
|
|
Re: Finding missing sequences [message #227876 is a reply to message #227298] |
Thu, 29 March 2007 23:11 |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
Quote: | Thanks for all the SQL's provided. They are working.
|
Quote: | Now how can you find the sequences?
|
Use the SQL which 'worked', only replace the value with the expression I provided (or modify it by your demands as you slightly changed them).
I would prefer to use the ebrian's solution.
|
|
|
|
|
|
|
|
|
Re: Finding missing sequences [message #227941 is a reply to message #227298] |
Fri, 30 March 2007 05:35 |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
jiteshmnair,
sorry, but I did not totally get, what you want.
First you ask for solution of a problem, get some solutions (better or worse), claim them working. Then ask only for a little format change of the stored column (as I understood) or for explanation of the way how rows are generated in the subquery containing CONNECT BY section (as others tried to show you).
SELECT '26SI06'||to_char((rownum+1-1),'fm0999' )
FROM dual
CONNECT BY rownum < 100010;
What does this code return to you? I do not have an access to Oracle, but I suspect it throws something like 'numeric overflow', as I guessed in my previous post. You can fix it when changing the last number from 100010 to 9999.
My advice stays the same: take the ebrian's solution and replace '26SI6D' with the prefix, you decided for, everywhere in the query (seems to be '26SI06' now).
By the way I do not believe the query you posted returned anything as it contains some syntax errors.
|
|
|