hive date format error when used with the table - sql

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

Related

SQLite - problem with "near « ) » : syntax error"

I try here since i didn't find anything anywhere else.
I have a query which has been check by my colleagues and "teachers", but the error remains:
" near « ) » : syntax error".
The (last) query is the following one:
WITH valeur_par_ville AS
(
SELECT dep_code, com_code, AVG(valeur_fonciere) as valeur
FROM vente
JOIN bien ON bien.bien_id = vente.bien_id
JOIN commune ON commune.com_id = bien.com_id
WHERE dep_code IN (6,13,33,59,69)
GROUP BY dep_code, com_id
)
SELECT dep_code AS "Département", com_id AS "Commune", round(valeur,1) AS "Prix moyen"
FROM(
SELECT dep_code, com_nom, valeur_fonciere,
rank() OVER (PARTITION BY dep_code ORDER BY valeur_fonciere DESC) AS
rang
FROM valeur_par_ville) AS result
WHERE rang <= 3
I hope you can help me on this, because it becomes "black magic" to me!
Thanks.
I tried different ways to reach a result, but the same thing keeps going... I Checked all my data tables, updated SQLite (so PARTITION BY works on it now), reboot, checked the query little by little... No idea where it comes from.
Others colleagues (we are kind of students) managed to have a valid result with their own queries.
But for me:
" near « ) » : syntax error".
The error is coming from a different part of the code. I added CREATE TABLE statements to be able to run your code and it's failing differently.
CREATE TABLE vente(dep_code, com_code, valeur_fonciere, bien_id);
CREATE TABLE bien(bien_id, com_id);
CREATE TABLE commune(com_id);
-- your code here
The error:
Parse error near line 4: ambiguous column name: com_id
RE dep_code IN (6,13,33,59,69) GROUP BY dep_code, com_id ) SELECT dep_code AS
error here ---^
After prefixing it with a table name, there's another bunch of errors, e.g.
arse error near line 4: no such column: com_nom
(valeur,1) AS "Prix moyen" FROM( SELECT dep_code, com_nom, valeur_fonciere, ra
error here ---^
Parse error near line 4: no such column: valeur_fonciere
) AS "Prix moyen" FROM( SELECT dep_code, com_nom, valeur_fonciere, rank() OVER
error here ---^
etc.

Nested select statements in impala sql

I have the following SQL query in impala
SELECT currentdate,close
FROM ( SELECT * FROM spyprice)
Where currentdate between '2015-01-16' and '2016-06-17';
And it is giving me the error:
Starting Impala Shell without Kerberos authentication
ERROR: AnalysisException: Syntax error in line 15:
WHERE currentdate BETWEEN '2015-01-16' and '2016-06-17'
^
Encountered: WHERE
Expected: AS, DEFAULT, IDENTIFIER
CAUSED BY: Exception: Syntax error
Anyone knows what's going on?
Thanks in advance!
#James Xiang The right syntax of the query statement is :
SELECT a.currentdate, a.close FROM
(
SELECT * FROM spyprice
) a
Where a.currentdate between '2015-01-16' and '2016-06-17';

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.

SQL Error: ORA-00917

I keep getting this error when attempting to submit a form to an Oracle DB saying I'm missing a comma, but I can't figure out where. Could it be that it's not referencing the code it shows below?
SQL Error: 917 - ORA-00917: missing comma
[query]
INSERT INTO request_maps
(
request_cn
,new_map
,new_title
,new_legend
,new_lat
,new_long
,new_alpha
,TO_DATE(new_pubdate, 'MM-DD-YYYY')
,new_compiled
,new_pubby
,new_metadata
,new_disclaimers
,new_key
,new_matrix
,new_recmatrix
,new_township
,free_title
,free_legend
,free_desc
,free_insets
,free_key
,free_long
,free_lat
,free_township
,free_utm
,request_maps_type_cn
,created_by
) VALUES (
:request_cn
,:new_map
,:new_title
,:new_legend
,:new_lat
,:new_long
,:new_alpha
,TO_DATE(:new_pubdate, 'MM-DD-YYYY')
,:new_compiled
,:new_pubby
,:new_metadata
,:new_disclaimers
,:new_key
,:new_matrix
,:new_recmatrix
,:new_township
,:free_title
,:free_legend
,:free_desc
,:free_insets
,:free_key
,:free_long
,:free_lat
,:free_township
,:free_utm
,:request_maps_type_cn
,:created_by
)
This is wrong:
INSERT INTO request_maps
(reqest_cn
,new_map7
,new_title
,new_legend
,new_lat
,new_long
,new_alpha
,TO_DATE(new_pubdate, 'MM-DD-YYYY') <-- change to new_pubdate
,new_compiled
You can't have an expression like that inside the field list...

Active Record query causing SQLException near "," syntax error

I'm trying to display all such rows of a table STUDENTS which have a value of an attribute, such as COURSE IN a set of given values e.g. 'MS', 'PhD'.
I get the values in the students_controller.rb file using params. I tried to run an Active Record query using where to do the job:
#all_courses = ['MS', 'PhD', 'BA', 'MSc']
#students = Student.where("course IN :courses" , {:courses => params.has_key?(:courses) ? params[:courses].keys : #all_courses})
But I get the following error:
SQLite3::SQLException: near ",": syntax error: SELECT "students".* FROM "students" WHERE (course IN 'MS', 'PhD', 'BA', 'MSc')
I think the error might be due to the absence of ; at the end of the SQL query generated by Active Record, but I cannot do anything to get that semicolon at the end.
You need to use parentheses: "course IN (:courses)"