Finding Missing Numbers

I want to find missing numbers from a column which is alpha numeric, its last five positions are numeric. At first three positions there are different combinations of alphas i.e. *'BA ','BAW','SA ','SAA' ..........* if alphas are two then 3rd position is blank. I also need it to generalised this to cover all existing alpha numeric series.
SELECT SUBSTR (a.claim_no, 4, 5) + 1
  FROM core_business.cb_pensioner a
WHERE SUBSTR (a.claim_no, 1, 3) = 'BA '
   AND NOT EXISTS (
                 SELECT NULL
                   FROM core_business.cb_pensioner b
                  WHERE to_number(SUBSTR (b.claim_no, 4, 5)) =  to_number(SUBSTR (a.claim_no, 4, 5)) + 1)
   order by SUBSTR (a.claim_no, 4, 5);I am getting ORA-01722 error

You have two issues with such a task.
Problem 1 is how to convert the trailing part into a number so that you can compare values.
to_number(SUBSTR (b.claim_no, 4, 5)) works, but only if the value in b.claim_no is always a number regardless of the other column criteria.
A statement like
WHERE SUBSTR (a.claim_no, 1, 3) = 'BA ' /* filter 1 */
and to_number(SUBSTR (b.claim_no, 4, 5)) > 1 /* filter 2 */
...might fail because your table might have a value like
b.claimno = 'XYZ1234b'
The way you wrote the query the database will decide which filter criteria it executes first. It might be filter1, but could also be filter2. If filter2 is executed first then you will get the error message.
Problem 2 how to order and compare different rows, so that you can find the missing values.
Here the analytic LEAD(LAG) function is very helpful. it will give you access to the next (or previous row).
Both problems can be overcome.
Here is my try, but I'm not sure if it works in your case. You might need to adapt a little.
This should work if all the rows are numeric that belong to the specific groups. if you can have wrong data in there too, then a different approach is needed.
with testdata as (select 'XYZ1234b' claim_no from dual union all
            select 'BA 12345' claim_no from dual union all
            select 'BA 12346' claim_no from dual union all
            select 'BA 12350' claim_no from dual union all
            select 'BAW11111' claim_no from dual union all
            select 'BAW11113' claim_no from dual union all
            select 'XYZ1234b' claim_no from dual )
/* end of test data creation */
select claim_no, substr(claim_no, 1,3) part1, substr(claim_no, 4) part2,
       to_number(substr(claim_no, 4)) current_num,
       lead(to_number(substr(claim_no, 4))) over (partition by substr(claim_no, 1,3) order by to_number(substr(claim_no, 4))) next_num,
       lead(to_number(substr(claim_no, 4))) over (partition by substr(claim_no, 1,3) order by to_number(substr(claim_no, 4)))
        - 1 - to_number(substr(claim_no, 4))
        as "missing_values"
from testdata
where substr(claim_no, 1,3) in ('BAW','BA ')
order by claim_no;
CLAIM_NO PAR PART2 CURRENT_NUM   NEXT_NUM missing_values
BA 12345 BA  12345       12345      12346              0
BA 12346 BA  12346       12346      12350              3
BA 12350 BA  12350       12350
BAW11111 BAW 11111       11111      11113              1
BAW11113 BAW 11113       11113Problem 1 is solved because the where condition is always executed before the function in the select clause, especially in connection with an analytic function.
Problem 2 is also solved by using the analytic function LEAD function. And this is fast, because you access the same table only one time. Other solution might do a subquery to fetch the value from the previous/next row. This means several accesses to the same table and is usually much slower.
Edited by: Sven W. on Sep 20, 2010 2:32 PM

Similar Messages

  • Find missing numbers in array

    HI !
    I have an array which holds 999 different numbers between 1-1000
    (which means that 1 number between 1-1000 does not appear in the array).
    I need to find the missing number in one pass and it's easy.
    My question is:
    Do you have any idea how can I find 2 missing numbers in the array ?
    Is it possible ?
    I cant use any data structure and I need to find the missing numbers in one pass.
    Thank you

    Using array.sort() and using another array is out of the question as its inhrently banned in the "i cant use any data structure" so this is
    Homework! tut tut
    ok so far an array with 1 number missing "its easy" yeah, just add em all up and see whats missing.
    for 2 numbers missing you should take a hint from what you are studying in the course ie solving Quadratic Equations..
    shall i make snoopy beg yet??
    naw, ok here goes
    on your 1 permitted pass add up all the mumbers that are there, also have another total for the squares of the numbers
    call the numbers you are looking for A and B
    subtract the first total from what it would be if there were all 1000, call this N
    subtract the second total from what the total of the squares would be if there were all 1000, call this M
    so..
    A+B=N
    solve for B in terms of A
    so..
    B=N-A
    and A^2+B^2=M
    should i quit now???
    no, ok
    substitute B in the second equation, shuffle a bit and..
    2A^2-2NA+N^2-M=0
    recognise the terms of the quadratic and..
    A=(2N +/- (4N^2-8(N^2-M))^.5)/4
    note the +/- plus OR minus will give you 2 results, these will be the 2 missing munbers
    tahh dahh
    See maths does have some use

  • How to find Missing Numbers, simplest way

    Hi All,
    Table 1
    Invoice_books_issues
    Columns are Transaction_date,serail_no_start,serial_no_end.
    Ex Data is :
    1/1/2005...........1000.............2000
    2/2/2005...........4500.............5500
    My Invoice_table is
    Inv_no
    1908
    1005
    4502
    4589
    5509......on and on...
    so i need to get the missing nos out of the issues. like
    1000
    1001
    1002
    1003
    1004 ... note here 1005 alrady been used.
    1006
    ... on and on.
    Thanks in advance.

    something like this :
    1. create view holding surrogate numbers:
    CREATE OR REPLACE VIEW NUM_VIEW
    (NUM)
    AS
    SELECT thous.x+huns.x+tens.x+ones.x num FROM
    (SELECT 0 x FROM dual
    UNION SELECT 1 FROM dual
    UNION SELECT 2 FROM dual
    UNION SELECT 3 FROM dual
    UNION SELECT 4 FROM dual
    UNION SELECT 5 FROM dual
    UNION SELECT 6 FROM dual
    UNION SELECT 7 FROM dual
    UNION SELECT 8 FROM dual
    UNION SELECT 9 FROM dual) ones,
    (SELECT 0 x FROM dual
    UNION SELECT 10 FROM dual
    UNION SELECT 20 FROM dual
    UNION SELECT 30 FROM dual
    UNION SELECT 40 FROM dual
    UNION SELECT 50 FROM dual
    UNION SELECT 60 FROM dual
    UNION SELECT 70 FROM dual
    UNION SELECT 80 FROM dual
    UNION SELECT 90 FROM dual) tens,
    (SELECT 0 x FROM dual
    UNION SELECT 100 FROM dual
    UNION SELECT 200 FROM dual
    UNION SELECT 300 FROM dual
    UNION SELECT 400 FROM dual
    UNION SELECT 500 FROM dual
    UNION SELECT 600 FROM dual
    UNION SELECT 700 FROM dual
    UNION SELECT 800 FROM dual
    UNION SELECT 900 FROM dual) huns,
    (SELECT 0 x FROM dual
    UNION SELECT 1000 FROM dual
    UNION SELECT 2000 FROM dual
    UNION SELECT 3000 FROM dual
    UNION SELECT 4000 FROM dual
    UNION SELECT 5000 FROM dual
    UNION SELECT 6000 FROM dual
    UNION SELECT 7000 FROM dual
    UNION SELECT 8000 FROM dual
    UNION SELECT 9000 FROM dual) thous
    this one holds numbers up to 9999 - should be up to the maximum value for your invoice number.
    2.
    CREATE TABLE INVOICE
    INV_NO NUMBER(5)
    Insert into invoice
    (INV_NO)
    Values
    (1000);
    Insert into invoice
    (INV_NO)
    Values
    (4501);
    Insert into invoice
    (INV_NO)
    Values
    (4505);
    COMMIT;
    3.
    CREATE TABLE INVOICE_BOOKS_ISSUES
    SERIAL_NO_START NUMBER,
    SERIAL_NO_END NUMBER
    Insert into INVOICE_BOOKS_ISSUES
    (SERIAL_NO_START, SERIAL_NO_END)
    Values
    (1000, 1006);
    Insert into INVOICE_BOOKS_ISSUES
    (SERIAL_NO_START, SERIAL_NO_END)
    Values
    (4500, 4507);
    COMMIT;
    4.
    SQL> select * from invoice;
    INV_NO
    4501
    1000
    1005
    4505
    SQL> select * from Invoice_books_issues;
    SERIAL_NO_START SERIAL_NO_END
    1000 1006
    4500 4507
    SQL>
    select num
    from num_view nv
    where exists
    (select serial_no_start, serial_no_end from Invoice_books_issues ibi
    where nv.num >= ibi.SERIAL_NO_START and nv.num <= ibi.SERIAL_NO_END)
    and
    not exists (select inv_no from invoice inv where inv.INV_NO = nv.num)
    NUM
    1001
    1002
    1003
    1004
    1006
    4500
    4502
    4503
    4504
    4506
    4507
    11 rows selected.
    SQL>
    hope this helped.
    Mike

  • Finding missed sequence numbers and rows from a fact table

    Finding missed sequence numbers and rows from a fact table
    Hi
    I am working on an OLAP date cube with the following schema:
    As you can see there is a fact transaction with two dimensions called cardNumber and Sequence. Card dimension contains about three million card numbers. 
    Sequence dimension contains a sequence number from 0 to 255. Fact transaction contains about 400 million transactions of those cards.
    Each transaction has a sequence number in 0 to 255 ranges. If sequence number of transactions of a card reaches to 255 the next transaction would get 0 as a sequence number.
    For example if a card has 1000 transactions then sequence numbers are as follows;
    Transaction 1 to transaction 256 with sequences from 0 to 255
    Transaction 257 to transaction 512 with sequences from 0 to 255
    Transaction 513 to transaction 768 with sequences from 0 to 255
    Transaction 769 to transaction 1000 with sequences from 0 to 231
    The problem is that:
    Sometimes there are several missed transactions. For example instead of sequence from 0 to 255, sequences are from 0 to 150 and then from 160 to 255. Here 10 transactions have been missed.
    How can I find all missed transactions of all cards with a MDX QUERY?
    I really appreciate for helps

    Thank you Liao
    I need to find missed numbers, In this scenario I want the query to tell the missed numbers are: 151,152,153,154,155,156,157,158,159
    Relative transactions are also missed, so I think it is impossible to get them by your MDX query
    Suppose this:
    date
    time
    sequence
    20140701
    23:22:00
    149
    20140701
    23:44:00
    150
    20140702
    8:30:00
    160
    20140702
    9:30:00
    161
    20140702
    11:30:00
    162
    20140702
    11:45:00
    163
    As you can see the sequence number of the last transaction at the 20140701 is 150
    We expecting that the first transaction of the next day should be 151 but it is 160. Those 10 transactions are totally missed and we just need to
    find missed sequence numbers

  • Finding missing sequence

    Hello all.
    I'm using the Oracle 10g Database. i'm trying to figure out how to write a simple sql query to:
    find the missing numbers in a table between say 86002895 and 86005197 (inclusive)
    Ex: Current Scenario : table_1 :
    tracking_no | id_value
    86002895 | 10
    86002896 | 10
    86002899 | 10
    86002900 | 10
    86002910 | 10
    86005196 | 10
    86005197 | 10
    Expected Result1:
    " missing tracking_id " where id_value = 10 from table_1 ;
    86002897
    86002898
    86002900 to 86002910
    86002910 to 86005196
    Thanks in advance :)

    user8635888 wrote:
    Ex: Current Scenario : table_1 :
    tracking_no | id_value
    86002895 | 10
    86002896 | 10
    86002899 | 10
    86002900 | 10
    86002910 | 10
    86005196 | 10
    86005197 | 10
    Expected Result1:
    " missing tracking_id " where id_value = 10 from table_1 ;
    86002897
    86002898
    86002900 to 86002910
    86002910 to 86005196
    Thanks in advance :)Maybe something like the following:
    SQL> SELECT * FROM TEST_TAB
      2  /
    TRACKING_NO   ID_VALUE
       86002895         10
       86002896         10
       86002899         10
       86002900         10
       86002910         10
       86005196         10
       86005197         10
    7 rows selected.
    SQL> SELECT   CASE
      2              WHEN tracking_no + 1 = lead_no - 1 THEN TO_CHAR (tracking_no +1)
      3              ELSE TO_CHAR (tracking_no + 1) || '-' || TO_CHAR (lead_no - 1)
      4           END
      5              Missing_track_no
      6    FROM   (SELECT   tracking_no,
      7                     LEAD (tracking_no, 1, NULL)
      8                        OVER (ORDER BY tracking_no ASC)
      9                        lead_no
    10              FROM   test_tab
    11             WHERE   ID_VALUE = 10)
    12   WHERE   lead_no != tracking_no + 1
    13  /
    MISSING_TRACK_NO
    86002897-86002898
    86002901-86002909
    86002911-86005195
    3 rows selected.
    SQL>You can tweak the above query to match your requirement.
    Hope this helps.
    Regards,
    Jo

  • Looking to write a query to give me the missing numbers in a sequence

    My query here gives me a list of numbers:
     select distinct cast(SUBSTRING(docket,7,999) as INT) from [DHI_IL_Stage].[dbo].[Violation] where InsertDataSourceID='40'
      and ViolationCounty='Carroll' and SUBSTRING(docket,5,2)='TR' and LEFT(docket,4)='2011'
      order by 1
    Gives back:
    I want to find the numbers missing in the sequence

    CREATE TABLE t1  (C INT NOT NULL PRIMARY KEY)
    insert t1 select 1 union all
     select 3 union all
    select 4 union all
    select 5 union all
    select 7 union all
    select 10 
    SELECT
      c+n FROM t1, (
      SELECT 0 n UNION ALL SELECT 1
    ) T
    GROUP BY c+n
    HAVING MIN(n) = 1 and c+n < (select max(c) from t1)
    Best Regards,Uri Dimant SQL Server MVP,
    http://sqlblog.com/blogs/uri_dimant/
    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Consulting:
    Large scale of database and data cleansing
    Remote DBA Services:
    Improves MS SQL Database Performance
    SQL Server Integration Services:
    Business Intelligence

  • Finding discontiguous numbers

    Greetings,
    I need to write a plsql scripts that will find discontiguous numbers. For example, say I have a table..
    id int
    store varchar
    tracking_num int
    Each store uses a different sequence for their tracking number. ii need to find and list the contiguous segments for each store, and the total number of items shipped. For example..
    STORE MIN MAX
    STORE X 100 105
    STORE X 107 110
    STORE X 112 115
    STORE X TOTAL 14
    STORE Y 901 903
    STORE Y 905 907
    STORE Y 908 910
    STORE X TOTAL 9
    OK, I figure I need a cursor to be able to do this. I figure I'll loop through each store's records, increment a counter for the total. For the discontinuous I figure I'll add a new column which I'll use to group the contiguous segments together, then I can just a max and min tracking number for each store's contiguous groups.
    The problem I'm having is wrapping my head around how to build a dataset with a cursor populating the new contiguous column IN MEMORY, in an line view or something, without having to create a physical table.
    Anyone have any suggestions, or see any flaws with this logic?

    Using Oracle 10.2.0.4
    I thought this was a simple enough question that I could get away without specific examples. Tha being said
    create table store_tracking (
    id  int,
    store varchar2(20),
    tracking int
    INSERT INTO STORE_TRACKING (SELECT 1, 'STORE X', 100 FROM DUAL );
    INSERT INTO STORE_TRACKING (SELECT 2, 'STORE X', 101 FROM DUAL );
    INSERT INTO STORE_TRACKING (SELECT 3, 'STORE X', 102 FROM DUAL );
    INSERT INTO STORE_TRACKING (SELECT 4, 'STORE X', 103 FROM DUAL );
    INSERT INTO STORE_TRACKING (SELECT 5, 'STORE X', 104 FROM DUAL );
    INSERT INTO STORE_TRACKING (SELECT 6, 'STORE X', 105 FROM DUAL );
    INSERT INTO STORE_TRACKING (SELECT 7, 'STORE X', 107 FROM DUAL );
    INSERT INTO STORE_TRACKING (SELECT 8, 'STORE X', 108 FROM DUAL );
    INSERT INTO STORE_TRACKING (SELECT 9, 'STORE X', 109 FROM DUAL );
    INSERT INTO STORE_TRACKING (SELECT 10, 'STORE X', 110 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 11, 'STORE X', 112 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 12, 'STORE X', 113 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 13, 'STORE X', 114 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 14, 'STORE X', 115 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 15, 'STORE Y', 901 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 16, 'STORE Y', 902 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 17, 'STORE Y', 903 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 18, 'STORE Y', 905 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 19, 'STORE Y', 906 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 20, 'STORE Y', 907 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 21, 'STORE Y', 908 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 22, 'STORE Y', 909 FROM DUAL);
    INSERT INTO STORE_TRACKING (SELECT 23, 'STORE Y', 910 FROM DUAL); Expected Output is ...
    STORE      MIN     MAX
    STORE X   100     105
    STORE X   107     110
    STORE X   112     115
    STORE X TOTAL 14
    STORE      MIN     MAX
    STORE Y   901     903
    STORE Y   905     907
    STORE Y   908     910
    STORE Y TOTAL 9The explaination of the output is that we need to identify the contiguous segments of tracking numbers for each store (and thereby implicitly indicating where we are missing tacking numbers), and the total number of tracking number for each store.
    I hope this helps. let me know if you have additional questions.
    Edited by: user7352432 on Apr 9, 2010 8:21 AM

  • "Open VI Reference" Option "Prompt user to find missing subVIs"

    I am using "Open VI Reference" to dynamically load a VI. For the "option" control of the "Open VI Reference" node, I pass "0x10" or "10" in hexadecimal, which supposedly "prompt user to find missing subVIs." Nevertheless, I don't get a prompt message dialog when I intentionally pass an invalid or missing VI path. Instead I just get an Error Code 7. What does this option "Prompt user..." suppose to do?

    Make sure you set the properties on the integer input into the “options” control to the “Open VI Reference” to be a Hexadecimal format (right-click on the “options” control input, select Format and Precision…, then select Hexadecimal). Now, if you re-run your VI and wire in a VI with a missing subVI, the Open VI Reference will appear with a dialog that will prompt you to browse to the missing subVI. This is exactly the same message LabVIEW displays when it attempts to open a VI and cannot find its subVIs.
    I’ve attached a simple example program to demonstrate. Enter the path to the “number extractor.vi” as the VI to open. You should see a dialog prompt appear asking for the “Search 1D Array – Complete.vi”.
    Hope this helps!
    Attachments:
    openVIRefExample.zip ‏20 KB

  • Property or method to find missing fonts...

    Hi Everyone,
    Is there any direct property or method to find missing fonts in EPS doucments in Illustrator CS4 like we do in InDesign. I have tried this by doing comparison of document fonts and application fonts to get missing fonts but it won't work.
    Any help would be appreciatable. Thanks in advance.
    Regards
    Thiyagu

    Have you tried a search of this forum… Im sure I posted a work around to this… The simple answer is NO not in the Illustrator DOM you can however read the file as text and the stuff you want is in the Illustrator file comments…

  • Is there not an easier way to find missing songs?

    Is there a smarter way to find missing songs and albums besides having to go through each one individually? Some kind of batch process would be nice...
    How do you guys efficiently find missing songs or albums?

    don't you just love the way Apple answers your questions in this forum?

  • Find no numbers  characters in a string

    HI, how i can find no numbers characters in a string
    By example.
    '45125454564#4545'.
    I only want to know if the string contains no number characters or not, i dont want to know the character '#'.
    Is there a function module for doing that?
    Thanks

    You can try:
    NUMERIC_CHECK
    OR
    IF var CO '0123456789'.
    ENDIF.

  • Finding missing photos when drive letter has changed

    I have over 30,000 missing photos because LR only recognizes drive letters not names.  All my drives have a master "Image" directory with folders and subfolders.  Going to the LR online directory with name change and right clicking images and finding folder location does not work.  LR does not recognize folders or subfolders in the drive with a name change.  Usually none of the folder names are high lighted. By going to the "Missing Photographs" in the catalog menu and searching by file name I can find individual files but with over 30k missing files, this approach is not practical. I have an HP computer with Windows 7 and LR 5.7.  How do I find the photos?

    I think the most straightforward way to sort this out is:
    1. Plug in all of your external drives.  From the screen shots, it looks like you have at most 5 external drives of interest, 2 that are currently online and 3 that are disconnected.  (I'm not counting the one external drive letter that has just one photo on it.)  How many external drives do you actually have?
    2. If you don't have enough USB ports, order a $12 USB hub from Amazon.
    3. Make sure the entire folder hierarchy for each drive is showing in the Folders pane as described here. When you're done, each drive in the Folders pane should have a top-level folder labeled with a drive letter (as with your F: drive in the screenshot above).
    4. Expand the entire folder hierarchy on each drive in the Folders pane by clicking the arrows to the left of the folder.
    5. For each drive, find one or more folders in its hierarchy that are closest to the top that have a "?" on them.  It may be the root folder labeled with the drive letter (e.g. "F:"), or it may be folders below the root folder.   For each such folder with a "?", right-click it and select Find Missing Folder.  That folder should be somewhere on one of the drives.  Use Windows File Explorer to help find where it is.
    6. When you're done with step 5, you should have located all missing folders and told LR where they are currently located.  But you may have missed some.   Do Library > Find All Missing Photos.
    7. Pick a photo from step 6.  Right-click it and do Go To Folder In Library.  If the folder in the Folder pane is marked with a "?", right-click it and do Find Missing Folder.   Otherwise, click the "!" on the photo and then click Locate.  Use Windows File Explorer to help find the missing photo by filename, if necessary.  Repeat until there are no more missing photos.
    8. Going forward, try to keep all your external drives connected.  To minimize drive-letter confusion in the future, rename the drive letters of the existing external drives to start from the end of the alphabet, changing each drive letter as described here.   When you change a drive letter, you'll need to re-locate its root folder as described in steps 3-5.   (Windows tends to use drive letters from C onward.)

  • My free trail for $29.99 as run out. want to join but i can't find serial numbers. help!

    my free trail for $29.99 as run out. want to join but i can't find serial numbers. help!

    Cloud programs do not use serial numbers... you download & install & activate by logging in to your paid Cloud account
    http://www.adobe.com/products/creativecloud/faq.html
    http://helpx.adobe.com/creative-cloud/help/install-apps.html
    http://forums.adobe.com/community/download_install_setup/creative_cloud_faq
    what is http://helpx.adobe.com/creative-cloud/help/creative-cloud-desktop.html
    Cloud Getting Started https://helpx.adobe.com/creative-cloud/topics/getting-started.html

  • LR5 Find missing photos - search nearby won't work consistently

    LR5 seems to work inconsistently when locating missing photos and selecting "find nearby photos".
    It has succeeded in finding nearby files but now will only locate one file at a time....a very annoying and time consuming exercise!
    Any suggestions please?

    Hmm, I'm still not sure about your situation.  But if the entire folder was missing (before you started finding individual pics), then it's easy to find the whole folder.  You can see if the folder is missing by looking in the Folders pane on the left -- it will have a "?" on it:
    Right-click on the folder and select Find Missing Folder.

  • How can I get two missing numbers on a iTunes card

    How can I get two missing numbers on a iTunes card

    "Get help",just below the form space where I entered the code. Once I clicked "Get Help", it added another line to the form where I could enter  THE NUMBER ON THE BOTTOM LEFT CORNER on the back of the card. I entered as much of the I scratch-off code I could read, along with the serial number.

Maybe you are looking for