LEAD function syntax in HIVE-QL - sql

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.

Related

What is the syntax problem here using this subquery inside where clause

SELECT p.pnum, p.pname
FROM professor p, class c
WHERE p.pnum = c.pnum AND c.cnum = CS245 AND (SELECT COUNT(*) FROM (SELECT MAX(m.grade), MAX(m.grade) - MIN(m.grade) AS diff
FROM mark m WHERE m.cnum = c.cnum AND m.term = c.term AND m.section = c.section AND diff <= 20)) = 3
Incorrect syntax near ')'. Expecting AS, FOR_PATH, ID, or QUOTED_ID.
Consider the following example:
SELECT COUNT(1)
FROM SYSCAT.TABLES T
WHERE
(
-- SELECT COUNT(1)
-- FROM
-- (
SELECT COUNT(1)
FROM SYSCAT.COLUMNS C
WHERE C.TABSCHEMA=T.TABSCHEMA AND C.TABNAME=T.TABNAME
-- )
) > 50;
The query above works as is. But the problem is, that if you uncomment the commented out lines, you get the following error message: "T.TABNAME" is an undefined name. and least in Db2 for Linux, Unix and Windows.
You can't push external to the sub-select column references too deeply.
So, your query is incorrect.
It's hard to correct it, until you provide the task description with data sample and the result expected.
I can see a potential syntax errors: It seems that CS245 refers to a value c.cnum may take and not a column name. If that is the case, it should be enclosed in single quotes.

Can't find ORA-00907: missing the right parenthesis

I don't know what is wrong with this syntax.
SELECT Count (id_conv) AS NUM_CAMP
FROM csd_mx_mae_camp_dro
WHERE id_conv = (SELECT id_conv
FROM csd_mx_mae_conv_dro
WHERE num_cta = 60385300500)
AND id_cncpt = (SELECT A.id_cncpt
FROM csd_mx_mae_camp_dro A
INNER JOIN csd_mx_mae_cncpt_dro B
ON A.id_cncpt = B.id_cncpt
WHERE ( ( flg_tipo_camp = 'A'
AND txt_nombr_clase_logic IS NOT NULL )
OR ( flg_tipo_camp = 'C' ) )
AND txt_nom NOT IN ( 'Concepto' )
AND B.txt_cve = '84'
AND A.id_conv = (SELECT id_conv
FROM csd_mx_mae_conv_dro
WHERE num_cta = 60385300500)
AND rownum = 1
ORDER BY id_cmp)
AND flg_tipo_camp = 'A';
The expected result is 4, taking into account my records in the DB, however I have the error mentioned in the title (ORA-00907: missing the right parenthesis
00907. 00000 - "missing right parenthesis"
* Cause:
* Action:
Error in the line: 171, column: 90).
There are some subqueries where ORDER BY makes sense - and it is allowed by the syntax.
However, you use ORDER BY in a scalar subquery - one that is required to return a single value (one row / one column), and such subqueries do not allow ORDER BY.
You are using it incorrectly anyway (most likely) - you limit the number of rows to 1 by the condition ROWNUM = 1, which in conjunction with your ORDER BY probably means you wanted to order by ID_CMP and then take the first row from the result. That is not how it works; ORDER BY comes only after ROWNUM is assigned anyway. If that's what you were trying to do, remove ORDER BY as well as the condition on ROWNUM, and instead select MIN(ID_CMP) in the SELECT clause of the scalar subquery.
The specific error about the missing right parenthesis is caused by the ORDER BY clause: at that point, in a scalar subquery, the parser expects the closing parenthesis for the subquery, not any other token/clause/whatever.

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.

Wrapping an analytical function in another function in Hive 0.11

I am trying the following:
select case when ta_end_datetime_berekenen = 'Y' then lead(ta_update_datetime) over ( partition by dn_waarde_van, dn_waarde_tot order by ta_update_datetime ) else ea_end_datetime end ea_end_datetime, ta_insert_datetime, ta_update_datetime from tmp_wtdh_bestedingsklasse_10_s2_stap2
However, when I try that, I get the following error:
NoViableAltException(86#[129:7: ( ( ( KW_AS )? identifier ) | ( KW_AS LPAREN identifier ( COMMA identifier )* RPAREN ) )?])
FAILED: ParseException line 1:175 missing KW_END at 'over' near ')' in selection target
line 1:254 cannot recognize input near 'else' 'ea_end_datetime' 'end' in selection target
Would I be correct in assuming that it's not possible to wrap an analytical function in another function?
This is with Hive 0.11.
Not sure this is the root of your problem, but seems like your query is missing an AS keyword (note the all-caps AS on line 8 below).
select
case
when ta_end_datetime_berekenen = 'Y'
then
lead(ta_update_datetime) over ( partition by dn_waarde_van, dn_waarde_tot order by ta_update_datetime )
else
ea_end_datetime
end AS ea_end_datetime,
ta_insert_datetime,
ta_update_datetime
from tmp_wtdh_bestedingsklasse_10_s2_stap2