error in hive query while using windowing - hive

While doing this
select
first_value(id) over(partition by id rows between 1 precending and 1 following)
from college;
query error is
FAILED: ParseException line 1:43 cannot recognize input near
'precending' 'and' '1' in window frame boundary

Related

PostgreSQL to PrestoSQL

I need to change my query from Postgres to Presto but I am getting an error which I don't understand - "mismatched input '&'. Expecting: expression"
My SQL query-
SELECT sub.custID, sum(coalesce(cast(devicecount as INT), 0)) as devicecount, data_attr3 FROM(
SELECT custID, data_attr1 as devicecount, split_part(data_attr3, '-', 2) data_attr3,
RANK() OVER
(
PARTITION by custID
ORDER BY collected_on desc
) AS rownum
FROM tableA
) sub
WHERE rownum = 1 and string_to_array(data_attr3, ':') && '{0,1,2,4,5,16}'::text[] group by 1, 3
I am getting an error at the last line where I am using "&&" but the error goes if I remove it.
The error then is not recognizing string_to_array and it cant recognize ::text[] on the last line. Any help would be appreciated.
As always - documentation is your friend. Presto has quite different syntax for handling arrays, so depending on Presto version you can try next:
select cardinality( -- count elements in array
array_intersect( -- get arrays intersection
split('1:2:3', ':'), -- split string on ':' as string_to_array
array['0','1','2','4','5','16']) -- create varchar array
) > 0;
or
select arrays_overlap(
split('1:2:3', ':'),
array['0','1','2','4','5','16']
);
Output:
_col0
true

Need a short simple aggregated function SQL query

I have a database base with a table named "extract1" with a column named "contactsphonenumber". I am trying to find what specific phone number appears the most.
Attempt 1:
SELECT MAX(COUNT(contactscellphone))
FROM extract1
GROUP BY contactscellphone;
Attempt 2:
SELECT MAX(contactscellphone)
FROM extract1
GROUP BY contactscellphone
(
SELECT COUNT(contactscellphone)
FROM extract1
GROUP BY contactscellphone
);
Attempt 1 Error:
Msg 130, Level 15, State 1, Line 1 Cannot perform an aggregate
function on an expression containing an aggregate or a subquery.
Attemp 2 Error:
Msg 156, Level 15, State 1, Line 6 Incorrect syntax near the keyword
'select'. Msg 102, Level 15, State 1, Line 8 Incorrect syntax near
')'.
Use TOP 1 and ORDER BY:
select top (1) contactscellphone
from extract1g
group by contactscellphone
order by count(*) desc;
If you want all top values when there are ties, use top (1) with ties.

ORA-00936 Missing expression on ROW NUMBER

I've checked linked questions and still haven't found an answer.
SELECT *
FROM
( SELECT contract_number,
ROW NUMBER() OVER (PARTITION BY contract_number
ORDER BY ID) RowNumber
FROM contracts ) a
WHERE a.RowNumber = 1
This code throws ORA-00936 error. It underlines ROW NUMBER(), what's missing here?
Have you got the syntax right for row number? It should be ROW_NUMBER()
in the 4th line ROW NUMBER() i used and in the last line you are using WHERE a.RowNumber = 1
the function name differ from each other
this is the only error

i try to combine rows in one column but there was an error in my syntax

select
Elm_EmployeeId as 'Badge',
Left(T_EmployeeLeave , Len(T_EmployeeLeave) - 1) As 'a'
from
(select
E2.Elm_EmployeeId as 'Badge2',
(select Elm_EmployeeId
from T_EmployeeLeave E1)
from
T_EmployeeLeave E2)
Error is:
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near ')'.
1.you select Elm_EmployeeId and T_EmployeeLeave from a subquery but in the subquery u don't have these two columns what you have is badge2 and a non_named column
the select Elm_EmployeeId from T_EmployeeLeave E1 is meaningless
the query is miserable i can't even tell what excatly you want

Complex SELECT IN doesn't work in Oracle 11

I have this query:
SELECT d.CREATION_DATE, r.RN5, s.rn1, s.PI1, r.KTR,
VERSION_NR, getTitle(r.RN5, VERSION_NR) AS TITLE
FROM DOI_SNAPSHOT d, RELATION r, SOURCE_ADDRESS s
WHERE r.RN1 NOT IN (8010,777)
AND d.RN5 = r.RN5
AND r.RN1 = s.RN1
AND r.RN5 IN (91010008,91010015)
AND r.RN5 not in (
SELECT RN5
FROM RELATION
WHERE DOI5 IS NOT NULL
AND DOI_DATE IS NULL
)
AND VERSION_NR = (
SELECT max(VERSION_NR)
FROM DOI_SNAPSHOT dmax
WHERE d.RN5 = dmax.RN5
);
and this query:
SELECT substr(w.message, 5, instr(w.message, 'KTR') - 5) AS RN5
FROM WEB_STATISTICS w
WHERE w.ACTION = 'DOI Display'
GROUP BY substr(w.message, 5, instr(w.message, 'KTR') - 5)
ORDER BY count(*) DESC;
both of them are working correctly.
Now if I replace the first query's line
AND r.RN5 IN (91010008, 91010015)
and put in the parenthesis the second query, I get an error
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
I don't know what is happening, since both queries are working oracle should at least accept the syntax?
The problem is that you have an order by clause in your second query, and you've left that in when you turned it into a subquery. The clause isn't valid in that context and makes no sense anyway - you're looking for members of a set, the ordering of the elements within that set is irrelevant - so you should remove the order by count(*) desc part.
To demonstrate with a simple but very contrived example; with an order by clause:
select * from dual
where dummy in (select 'X' from dual order by 1 desc);
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
And without:
select * from dual
where dummy in (select 'X' from dual);
DUMMY
-----
X
As a simplistic explanation of the error message, when the parser see the order by it thinks that applies to the main query, and therefore the subquery should have already been terminated a closing right parenthesis - which is missing.
This is a message that often doesn't mean what you might think it implies - it isn't that the query overall has unbalanced parentheses, just that it's found a syntax error that may have been caused by a missing or misplaced one.
Generally error ORA-00907 usually means "there is a syntax error somewhere before this point".
Let's try
SQL> select * from dual
2 where dummy in (select dummy from dual);
DUMMY
-----
X
SQL> select * from dual
2 where dummy in (select dummy from dual order by 1);
ORA-00907: missing right parenthesis
So you should remove ORDER BY clause from inner query.