SQL script returning FAILED: SemanticException in Hive - sql

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

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.

Hive - How to get results from 'describe table' into temp table or cte

I'm trying to run describe on a bunch of hive tables and would like to create a bunch of cte's and then UNION them. Something like this:
With t1 as (describe sch1.tab1)
, t2 as (describe sch1.tab2)
, t3 as (describe sch1.tab3)
select *
from t1 UNION
select *
from t2 UNION
select *
from t3
Unfortunately I'm getting this error when I try to do so:
Error while compiling statement: FAILED: ParseException line 1:15 Failed to recognize predicate 'describe'. Failed rule: 'identifier' in table name
Any suggestions for how to do this?
You can add all describe commands in a .hql and use sort -u command to perform a UNION.
hive -f desc_tables.hql | sort -u

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

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.

Delete a record in Hive based on a table records in another database

I have two hive databases namely db1 and db2. I have a table in db1 called table1, and a table in db2 called table2. I want to delete some rows in table2 based on particular column values in table1.
I use the below query but it fails
DELETE FROM db2.table2
WHERE db2.table2.F_SESSION IN (
SELECT F_SESSION FROM db1.table1
WHERE db1.table1.STATUS = 1);
The error is
Error while compiling statement: FAILED: SemanticException [Error 10004]: Line 4:12 Invalid table alias or column reference 'db1': (possible column names are: f_session, status)
Any clue on where I am going wrong? Btw, I am not an experienced SQL person.
Try this,
DELETE FROM db2..table2
WHERE db2..table2.F_SESSION IN (
SELECT F_SESSION FROM db1..table1
WHERE db1..table1.STATUS = 1);
But this worked
USE db2;
DELETE FROM table2 WHERE table2.F_SESSION IN (
SELECT F_SESSION FROM db1.table1 AS T1
WHERE T1.STATUS = 1);

sql join no results error in amazon redshift

This seems like a simple join in amazon redshift that I have done countless times, but it gives me the following error:
'No results were returned by the query. [SQL State=02000]'
Here is the sql:
select
campaign_id as product_campaign_id,
count(*) as sends
into table_1
from customer_to_product_campaign
group by
product_campaign_id
;
select
product_campaign_id,
count(*) as opens_total
into table_2
from product_action_oc
where
product_action_type_paid = 'open'
group by
product_campaign_id
;
select
t1.product_campaign_id,
t1.sends,
t2.opens_total
into table_3
from table_1 t1
left join table_2 t2
on t1.product_campaign_id = t2.product_campaign_id
;
Additional info:
-table 1 (which is created without error) is ~ 6K rows
-table 2 (which is created without error) is ~ 10K rows
-the tables do have common product_campaign_id's, not that it should matter
thanks
This isn't an error, just a friendly Red Shift notification that there are no results being returned to the GUI. Basically, the into statement is absorbing all the results, so nothing is being returned.
Here is an example. Consider:
select *
into dum
from (select 1 as col) t;
This returns the same notification. But the data is there. Just do:
select *
from dum;
to see the one row just inserted.
(Note: I just tested this on Red Shift.)