oracleselectinsertora-00904

Seeing Error When Inserting Multiple Select Statements into Table


When including multiple select statements in my insert into a table, I am seeing the 00904: invalid identifier error

Does anyone know why, or how to modify this INSERT query so it would work?

(i) I have confirmed that the columns are valid and both exist

(ii) The individual select statements execute without issues when run separately

(iii) The select statements result in single values of the NUMBER data type

(iv) Column 4 Line 21 mentioned in the error message happens to be at the F2 position which is shown as --> F2 in the INSERT statement below

INSERT INTO
    VCHR_DASHB_SUMMARY_STATS (CurrWkCount, YEARTODATE)
SELECT 
    F1.CurrWkCount, -->F2.YEARTODATE   
FROM
(
    SELECT COUNT(VOUCHER_FILED_DATE) AS COUNT, SYSDATE AS "AS OF" 
    FROM CONTINUED_CLAIMS
    WHERE VOUCHER_FILED_DATE >= trunc ( sysdate, 'iw' )
    AND VOUCHER_FILED_DATE  < trunc ( sysdate, 'iw' ) + 5
) F1
CROSS JOIN
(
    SELECT COUNT(VOUCHER_FILED_DATE)
    FROM CONTINUED_CLAIMS
    WHERE voucher_filed_date >= trunc(sysdate, 'year')
) F2;

Error at Command Line : 4 Column : 21 Error report - SQL Error: ORA-00904: "F2"."YEARTODATE": invalid identifier 00904. 00000 - "%s: invalid identifier"


Solution

  • F2 is an inline view and it doesn't contain YEARTODATE column; its only column is count(voucher_filed_date) which doesn't even have an alias so ...

    The same goes for f1.currwkcount - there's no such column there. You used alias count (you shouldn't be doing it; it is the name of Oracle's function and makes confusion) and "AS OF" (exactly like that: upper case, two words, enclosed into double quotes - you can't use it any other way).

    Consider something like this (note that I don't have your tables so I can't test it). I presumed that count(voucher_filed_date) represents yeartodate; if not, what is it, then?

    insert into vchr_dashb_summary_stats (currwkcount, yeartodate)
    select f1.currwkcount, f2.yeartodate   
    from (select count(voucher_filed_date) as currwkcount, 
                 sysdate as as_of
          from continued_claims
          where voucher_filed_date >= trunc ( sysdate, 'iw' )
            and voucher_filed_date  < trunc ( sysdate, 'iw' ) + 5
         ) f1
    cross join
        (select count(voucher_filed_date) as yeartodate
         from continued_claims
         where voucher_filed_date >= trunc(sysdate, 'year')
        ) f2;