ParseError in CTE - Hive - hive

CTE is giving parseError : ParseException line 1:42 mismatched input 'as' expecting ) near 'cust_xref_id' in statement
with table1 as
(select 2020.cust_xref_id as 2020_cust, 2019.cust_xref_id as 2019_cust
from dea.sp_2020_us as 2020
left join dea.sp_2019_us as 2019
on 2020.cust_xref_id = 2019.cust_xref_id)
select count(distinct(*))
from tabel1
where 2019_cust is not null;
Can't figure out where the mistake is.
Any help would be appreciated!

If the column's name does not start with a string, you have to enclose the name with backticks (`2020_cust`).

Related

Create a new table with the inner join data in hive - I am getting an error

SELECT
stock_prices_uday.trading_date, stock_companies_uday.symbol,
stock_companies_uday.company_name, stock_companies_uday.headquarter,
stock_companies_uday.sector, stock_companies_uday.sub_industry,
stock_prices_uday.open, stock_prices_uday.close,
stock_prices_uday.low, stock_prices_uday.high
INTO
JOIN1_UDAY
FROM
STOCK_PRICES_UDAY
INNER JOIN
STOCK_COMPANIES_UDAY ON stock_companies_uday.symbol = stock_prices_uday.symbol;
and I'm getting an error:
Error while compiling statement: FAILED: ParseException line 4:1 cannot recognize input near 'INTO' 'JOIN1_UDAY' 'FROM' in expression specification
Hive doesn't support INTO. Use CREATE TABLE AS:
CREATE TABLE JOIN1_UDAY as
SELECT stock_prices_uday.trading_date, stock_companies_uday.symbol ,stock_companies_uday.company_name,
stock_companies_uday.headquarter,stock_companies_uday.sector,stock_companies_uday.sub_industry,stock_prices_uday.open,
stock_prices_uday.close,stock_prices_uday.low,stock_prices_uday.high
INTO JOIN1_UDAY
FROM STOCK_PRICES_UDAY INNER JOIN
STOCK_COMPANIES_UDAY
ON stock_companies_uday.symbol = stock_prices_uday.symbol;

ERROR missing EOF at 'select'

Why do I get this error? I'm trying to do queries and for this i need of several selects from different tables, without any combination, but I'm getting this error and I don't known why.
Syntax error(s) [missing EOF at 'select']
Code:
select
d.tdok typ,
d.nr numer,
d.symbmg symbol,
d.data dataDok,
d.dokumwe dokumWe,
d.datawe datawe,
d.idkntrh idKth,
d.kwotadok kwotadok,
d.kwotavat,
k.nazwa1 nazwa1,
k.nazwa2 nazwa2
from dokum0 d
join kontrahent0 k on d.idkntrh=k.idkntrh
select t.kwota_n kwota_n
from dokumterm t
where t.dokum_id = id_dokum;
select p.id_pozdok0 AS settledCount
from pozdok p
where p.dokum_id = id_dokum;
select p.id_pozdok0 AS posCount
from pozdok p
where p.dokum_id=id_dokum;
select t.kupspr kupspr
from sltdok t
where t.tdok=tdok;
Can anyone help me?
You dont have semicolon (;) after end of first select... So this:
join kontrahent0 k on d.idkntrh=k.idkntrh
should be:
join kontrahent0 k on d.idkntrh=k.idkntrh;

hive date format error when used with the table

I have fields like date_column = 20140228 in the table 1. When I hard code the number like below it works, but when I specify the column name its failing. With error
H110 Unable to submit statement. Error while compiling statement: FAILED: ParseException line 2:1 cannot recognize input near 'select' 'date_format' '(' in select clause [ERROR_STATUS]
Working:
select date_format(from_unixtime(unix_timestamp(cast('2014022817' as string),'yyyyMMddHH')),'yyyy-MM-dd HH');
Failing:
select
select date_format(from_unixtime(unix_timestamp(cast(date_column as string),'yyyyMMddHH')),'yyyy-MM-dd HH')
from
table1
Why are you repeating the select? Try this:
select date_format(from_unixtime(unix_timestamp(cast(date_column as string
),'yyyyMMddHH'
)
),'yyyy-MM-dd HH'
)
from table1

select inside select doesn't work in hive

The next query would work in oracle, but not in hive:
select user_key,(sum(333)/(select 10 from table.dual)) calculationResult from user_usage_table group by user_key;
The result I expect:
user_key calculationResult
DB-_app6_61_28fba6e2f0_12d 2930.4
DB-_app6_61_28fba6e2f0_171 2930.4
DB-_app6_61_28fba6e2f0_1b5 2930.4
DB-_app6_61_28fba6e2f0_69 2930.4
DB-_app6_61_28fba6e2f0_e9 2930.4
what do I get:
FAILED: ParseException line 1:33 cannot recognize input near 'select' '10' 'from' in expression specification
How do I apply this in hive?
Your query is quite strange. Why not just write:
select user_key, (sum(333) / 10) as calculationResult
from user_usage_table
group by user_key;
This should work in both Oracle and hive.

LEAD function syntax in HIVE-QL

Is there any way to convert below LEAD function into HIVE QL format??
NVL(LEAD(START_DT) OVER (PARTITION BY EV_ID,AR_EV_RLTNSHP_TYPE_CD ORDER BY START_DT)-1,'2099-12-31') AS DERIVED_END_DT
PFB the error:
FAILED: ParseException line 1:1599 missing ) at 'OVER' near '(' in
subquery source line 1:1603 missing FROM at '(' near '(' in subquery
source line 1:1604 cannot recognize input near 'PARTITION' 'BY'
'EV_ID' in subquery source
It is complicated in HiveSQL but you can do it with a left join and aggregation:
select t.ev_id, t.ar_ev_rltnshp_type_cd, t.start_date,
coalesce(min(tnext.start_dt) - 1, '2099-12-31') as derived_end_dt
from table t left join
table tnext
on t.ev_id = tnext.ev_id and t.ar_ev_rltnshp_type_cd = tnext.ar_ev_rltnshp_type_cd and
tnext.start_date > t.start_date
group by t.ev_id, t.ar_ev_rltnshp_type_cd, t.start_date;
This makes certain assumptions about start_date being unique within a given group, but it will probably work for your purposes.