Oracle : Query Runs Successfully, but the column name is not available - sql

In our application Oracle(11g and 12C), I'm facing a weird Issue as below.
When I run the below query in Oracle DB it runs successfully and gives me output.
select * from table1 where col1 in (select col2 from table2 ) ;
But when I run the below inner query alone it throws the error:
select col2 from table2 ORA-00904: "COL2": invalid identifier
When I described the table table2, Col2 is not there. The error is expected. But the previous query executes successfully which is my concern.Could some one explain me this behavior?

Always, I repeat always give and use table aliases. You will never run into troubles and won't face such scenarios:
Now run below query again in same DB, I'm sure it will throw some error:
select * from table1 a where a.col1 in (select b.col2 from table2 b) ;
Now notice, all I did was give tables aliases. What most probably happening here is that table1 has a column named 'col2' and sub-query is referencing to that one. That's why it runs fine and doesn't show any error.

Related

Hive "with" clause syntax

The with syntax is absolutely not cooperating, can not get it to work. Here is a stripped down version of it
set hive.strict.checks.cartesian.product=false;
with
newd as (select
avg(risk_score_highest) risk_score_hi,
avg(risk_score_current) risk_score_cur,
from table1),
oldd as ( select
avg(risk_score_highest) risk_score_hi,
avg(risk_score_current) risk_score_cur,
from table2
where ds='2022-09-08')
select
(newd.risk_score_hi-oldd.risk_score_hi)/newd.risk_score_hi diff_risk_score_hi,
(newd.risk_score_cur-oldd.risk_score_cur)/newd.risk_score_cur diff_risk_score_cur,
from newd cross join oldd
order by 1 desc
Apache Hive Error
[Statement 2 out of 2] hive error: Error while compiling statement:
FAILED: SemanticException [Error 10004]: Invalid table alias or
column reference 'newd': (possible column names are:
diff_risk_score_hi, diff_risk_score_cur)
I had been following the general form shown here: https://stackoverflow.com/a/47351815/1056563
WITH v_text
AS
(SELECT 1 AS key, 'One' AS value),
v_roman
AS
(SELECT 1 AS key, 'I' AS value)
INSERT OVERWRITE TABLE ramesh_test
SELECT v_text.key, v_text.value, v_roman.value
FROM v_text JOIN v_roman
ON (v_text.key = v_roman.key);
I can not understand what I am missing to get the inline with views to work.
Update My query (the first one on top) works in Presto (but obviously with the set hive.strict.checks.cartesian.product=false; line removed). So hive is really hard to get happy for with clauses apparently. I tried like a dozen different ways of using the aliases.

Using NOT, MIN, AND in SQL having clause creating error

I am working on Ingres9.2 version. When I execute a query like
select col1 from table1 group by col1 having not((5=min(col1)) and (0 = 1))
it raising an error as:
bad select or subselect target list has been found.
But the error not occurring if I change the query with any one of following conditions:
0=0 or 1=1 instead of 0=1.
Removing not.
Using or instead of and.
Removing min.
I am not able to find the reason for this behaviour. And the same error not occurring in other database which is also in Ingres. If anyone knows the reason, please explain it.
Ingres is an old database. Maybe it has a problem with using an aggregation function on a group by key. This is speculation, but this should work:
select col1
from table1
where not (5 = col1) and (0 = 1))
group by col1 ;
Of course, the condition would be more likely written as col1 <> 5, but the two forms are equivalent.

SQL script returning FAILED: SemanticException in Hive

My query is below:
SELECT Round (sum(column1),2) AS alias, column 2, column3
FROM table1
INNER JOIN table 2
ON table1.column3 = table2.acolumn3
Group by column3
Hive keeps returning the following error:
Error while compiling statement: FAILED: SemanticException Column column3 Found in more than One Tables/Subqueries
I have read several threads related to this error message. Theses scripts are usually quite complex. Mine is very simple. Cannot understand why Hive is throwing this message on such a simplistic script.
Try this:
SELECT Round (sum(column1),2) AS alias, tab1.column2 as col2, tab2.column3 as col3
FROM table1 tab1
INNER JOIN table2 tab2
ON tab1.column3 = tab2.acolumn3
Group by tab2.column3

Merge Query in DB2

I need to update few columns in one table with the very convoluted calculation.
I'm not good enough in SQL so I tried to use "with" clause in combination with update, but It threw error.
Then I found a post online which suggested to use MERGE so I came up with Merge query. But that one was also throwing an error.
So I removed all other column and updating only one column to remove complexity, but no avail still errors
Below is my query, select query inside working perfectly fine.
Please suggest.
MERGE INTO TABLE_1 AS O
USING (
SELECT ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE)) - ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE))*TO_NUMBER(TABLE_1.AUC_MILEAGE))/100000 ) as CORRECT_FLOOR_PRICE
FROM TABLE_1, TABLE_2,TABLE_3
WHERE TABLE_2.Primary_ID= TABLE_1.Primary_ID
AND TABLE_2.option_code = 'FSDS'
AND TABLE_1.FLOOR_PRICE <> '0.00'
and TABLE_3.Primary_ID=TABLE_1.Primary_ID
and TABLE_3.Primary_ID=TABLE_2.Primary_ID
) AS CORRECT
ON(
O.Primary_ID = CORRECT.Primary_ID
)
WHEN MATCHED THEN
UPDATE
set O.FLOOR_PRICE =CORRECT.CORRECT_FLOOR_PRICE
Error is
An error occurred when executing the SQL command:
MERGE INTO ........
DB2 SQL Error: SQLCODE=-199, SQLSTATE=42601, SQLERRMC=SELECT;VALUES, DRIVER=3.61.75 [SQL State=42601, DB Errorcode=-199]
Try this instead, I think you forgot your identifier in your select statement because after the "ON" statement "CORRECT.Primary_ID" doesn't associate to anything.
MERGE INTO TABLE_1 as O
USING (
SELECT ((TO_NUMBER(TABLE_3.Total_Whsle_Price)-TO_NUMBER
(TABLE_2.OPT_BASE_WHSLE)) - ((TO_NUMBER(TABLE_3.Total_Whsle_Price)
-TO_NUMBER(TABLE_2.OPT_BASE_WHSLE))*TO_NUMBER
(TABLE_1.AUC_MILEAGE))/100000 ) as CORRECT_FLOOR_PRICE,
TABLE_1.Primary_ID AS Primary_ID
FROM
TABLE_1, TABLE_2,TABLE_3
WHERE TABLE_2.Primary_ID = TABLE_1.Primary_ID
AND TABLE_2.option_code = 'FSDS'
AND TABLE_1.FLOOR_PRICE <> '0.00'
AND TABLE_3.Primary_ID=TABLE_1.Primary_ID
AND TABLE_3.Primary_ID=TABLE_2.Primary_ID
) AS CORRECT
ON(
O.Primary_ID = CORRECT.Primary_ID
)
WHEN MATCHED THEN
UPDATE
set O.FLOOR_PRICE = CORRECT.CORRECT_FLOOR_PRICE

Oracle equivalent of INSERT IGNORE

I found a very similar topic on Oracle Equivalent to MySQL INSERT IGNORE?
However, I could not make work any of the proposed solutions. My case is a little special as my table does contains only 1 field, which is the primary key. Let's call the field "id" and the table "myTable" in the following.
Using MERGE
merge into myTable t1 from (
select 42 as the_pk_value, 'TEST' as some_column from dual
) t2 on (t1.id = t2.the_pk_value)
when not matched then
insert (id) values (t2.the_pk_value);
This first attempt gives a SQL Error: ORA-02012: missing USING keyword error message. I have to admit that I did NOT understand the proposed syntax, so maybe I messed something when adapting to my pk-only-table. But as the showed example did not use any USING keyword, I dont understand where my error can be.
Using hint
insert /*+ ignore_row_on_dupkey_index(SPSECU, PK_SPSECU) */ into myTable (id) values ('TEST');
This does work through SQL Developer, however it does not work from Java (I suspect OJDBC Driver to remove the comments to reduce transfer size. BTW I could not figure out which JDBC driver I'm using... the Spring Tool Source bundle seems to connect without any further configuration to the Oracle Database. I see only a DERBY default driver installed.
Using NOT EXISTS
I could not make this syntax work. Here is what I wrote :
insert into myTable (id) values ('TEST') where not exists (select id from myTable where id='TEST');
I get an SQL Error: ORA-00933: SQL command not properly ended error with this version.
Using insert select
I did not understand anything of the proposed solution by knagaev... trying to adapt it to my table gave me this :
insert into myTable t1 select id from myTable t2 where not exists (select 1 from t1 where t1.id = 'TEST');
Can someone help me ? I'm used to MySQL INSERT IGNORE simple syntax and am quite new on Oracle (using 11g version).
Merge uses using instead of from:
merge into myTable t1 USING (
select 42 as the_pk_value, 'TEST' as some_column from dual
) t2 on (t1.id = t2.the_pk_value)