ORA-00904: "ROW_ID": invalid identifier - sql

I wanted to get the rowId from the below query, but when I execute the query I get an error.
WITH DATA_TBL AS
(SELECT
/*+ parallel(scr,10)
parallel(el,10) */ scr.rowid AS "row_id", scr.*
FROM SOURCE_TABLE scr
LEFT OUTER JOIN CITY el ON (el.CITY_NAME = scr.SUN_W)
WHERE el.rowid IS NULL
AND scr.SUN_W IS NOT NULL
AND GREATEST(scr.SUN_WA, scr.B ) IS NULL
)
SELECT row_id FROM DATA_TBL;
When I execute the above query I get the below error, I am not sure what I am doing wrong.
ORA-00904: "ROW_ID": invalid identifier
00904. 00000 - "%s: invalid identifier"
But the query runs without any issue if I do the below, but I wanted to select only row_id.

Do not use double quotes for identifiers! By default, Oracle upper cases all identifiers. You have explicitly defined it to be lower-case.
You could say select "row_id", but that is silly. Remove the double quotes in the CTE.

Related

Oracle SQL NVL() Function

Ques : How do I will select full table with "NVL()" Oracle SQL function
Code :
SELECT NVL(ORDERNO, 'AAA') * FROM GNGRB.PRBOOK;
Error :
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Error at Line: 11 Column: 30
Is this what you want?
SELECT NVL(ORDERNO, 'AAA') as NEWORDERNO, p.* FROM GNGRB.PRBOOK p;
This selects all rows and columns of the table, and adds another column, called ORDERNO, that replaces null values in ORDERNO with 'AAA'.
Note that this only works as long as ORDERNO is of a string-like datatype: both arguments of NVL() must have the same datatype.

Sql query not executing - showing invalid identifier error

with ticket_orders AS(
select REGEXP_SUBSTR(st.ORDER_ID, '\d{3}-\d{7}-\d{7}') "new_order_id"
from
(select
REGEXP_SUBSTR(details, 'Order\s+ID(\s*\(s\))?:\s*\d{3}-\d{7}-\d{7}') AS "ORDER_ID"
from o_remedy ort
) st)
select new_t.new_order_id from ticket_orders new_t;
I'm trying to execute the following sql statement but it keeps showing error as "NEW_T"."NEW_ORDER_ID": invalid identifier
How to get this "new_order_id" because I need to join it further.
Either remove double quotes from the derived column name, or use double quotes in select clause as well.
select new_t."new_order_id" from ticket_orders new_t;
By default, Oracle convert every object name to upper case, unless you specify it in double quotes. So "new_order_id" will not be converted to upper case, but while selecting new_t.new_order_id will be converted to upper case, which doesn't exist.
To resolve it, either remove double quotes from the derived column name, or use double quotes in select clause as well.
I replicated it like this on my local machine.
This will not work.
with tbl as
(select empno "new_emp_no" from emp)
select t.new_emp_no From tbl t
And give
ORA-00904: "T"."NEW_EMP_NO": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 116 Column: 10
But this will work.
with tbl as
(select empno "new_emp_no" from emp)
select t."new_emp_no" From tbl t
WITH ticket_orders AS
(
SELECT
REGEXP_SUBSTR(st.ORDER_ID, '\d{3}-\d{7}-\d{7}') new_order_id
FROM
(
SELECT
REGEXP_SUBSTR(details, 'Order\s+ID(\s*\(s\))?:\s*\d{3}-\d{7}-\d{7}') AS ORDER_ID
FROM o_remedy ort
) st
)
SELECT new_t.new_order_id FROM ticket_orders new_t;

Oracle Pivot Multi Table

I am trying to create a Pivot in Oracle. I keep getting the error message
ORA-00904: "VALUEZ": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 18 Column: 6
Any thoughts?
SELECT * FROM
(
SELECT ACC.NBR,CTA.NAMEZ
FROM ACCS ACC
JOIN CARS CAR ON CAR.CAR_AAD_ID = ACC.ACC_AAD_ID
JOIN CTAS CTA ON CAR_CUS_ID = CTA_CUS_ID
)
PIVOT
(
MAX(VALUEZ) --comes from table CTAS (ERROR LINE)
FOR NAMEZ IN ('1','2','3') --from table CTAS
)
ORDER BY ACC.NBR;
As a side note, I would love it if it was possible to turn the ('1','2','3') into a subquery, but it looks like that is not possible from other post i have read. If it was easy it would be (select distinct namez from CTAS)
The columns referenced in your PIVOT clause must exist in the row source that is being pivoted. You select ACC.NBR,CTA.NAMEZ from the table; it looks like you need to expand that to ACC.NBR,CTA.NAMEZ,CTA.VALUEZ.
You cannot use a subquery to replace the list of pivot values. I believe the underlying reason for this is that the parser must be able to figure out the columns that the query will produce prior to executing it; so the pivot values must be hardcoded.
What you might be able to do, if it is appropriate to wrap this query up in a procedure or function, is to first execute a query to get the list of pivot values, then build the pivot query string using that information and execute it via dynamic SQL.
ora-01748: only simple column names allowed here pivot
select* from (SELECT TRUNC(I.POST_DATE) DATES FROM INVOICE I
INNER JOIN INVC_TENDER_V T ON T.INVC_SID=I.INVC_SID)
PIVOT
(COUNT(*)
FOR T.TENDER_TYPE IN(0,1) )

Invalid identifier error when all listed elements exist

I'm doing a basic database assignment on ETL. I'm trying to update a column by referencing three distinct columns in two other tables.
I am getting this error report in SQL Developer:
SQL Error: ORA-00904: "DIMTIME"."DAY_TIME": invalid identifier
00904. 00000 - "%s: invalid identifier"`
I feel my syntax is bad as all listed elements exist.
Code snippet is:
update fact_stage set date_sk = (
select date_sk from time_stage
where (time_stage.year_time = dimtime.year_time)
and (time_stage.month_time = dimtime.month_time)
and (time_stage.day_time = dimtime.day_time)
);
As I understand you want to add some ID from time_stage table into date_sk column in fact_stage table. I propose to change sql query to something like
update fact_stage set date_sk = (
select ID -- identifier column in time_stage table
from time_stage dim join fact_stage fact
on (dim.year_time = fact.year_time
and dim.month_time = fact.month_time
and dim.day_time = fact.day_time)
-- or instead of ON part use "USING (year_time, month_time, day_time)"
);
hope it helps

Missing Expression when selecting all columns and one more

This kind of query would work perfectly in SQL Server, but it does not work in Oracle.
select issueno, * from SOMETABLE;
The error message I'm getting is:
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action: Error at Line: 1 Column: 16
What is wrong?
Try this, when working with oracle db you need alias when you use column name along with *
select issueno, A.* from SOMETABLE A;
On Oracle you have to include the table name or an alias to use the *. Try this:
select issueno, SOMETABLE.*
from SOMETABLE;