Active Record query causing SQLException near "," syntax error - sql

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)"

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.

Trying to get a sql query working in PowerShell

I am working on trying to pull information from a DB and compare against A.D. users, but cannot seem to get the syntax correct.
I am pulling all the users in A.D. where EmployeeID is not null (just the EmployeeID itself), and then converting to a string:
$aUsers = (Get-ADUser -LDAPFilter "(employeeID=*)" -Properties EmployeeID).EmployeeID
$b = ($aUsers -join ",")
I am then attempting to query the table for any users in the db that do NOT have a corresponding EmployeeID in A.D.:
"Select * from PS_WR_EE_CWR_CACHE Where EMPLID NOT IN (" + $b + ")"
But this gives me the error "Incorrect syntax near ','. If I do something manually, however, like so:
Select * from PS_WR_EE_CWR_CACHE Where EMPLID NOT IN (110, 111, 118)
it works perfectly. Not sure what in the syntax I am getting wrong that it won't take the list.

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

Derby DB Insert Error SQL state 21000

I am attempting to insert data into a table in my database. I am using an Oracle Apache Derby DB. I have the following code-
Insert into P2K_DBA.ODS_CNTRL
(ODS_LOAD_ID, ODS_STATUS, USR_WWID, USR_FIRST_NM,
USR_LAST_NM, USR_DISPLAY_NM, USR_NT_ID,TOT_AMT,
TOT_RCD_CNT, TOT_QTY, LAST_UPD_DT, ODS_ADJ_TYP,
ODS_ADJ_DESC, APRV_WWID, APRV_FIRST_NM,APRV_LAST_NM,
APRV_DISPLAY_NM, APRV_NT_ID, APRV_DT
)
values
(6,'avail','64300339', 'Travis',
'Taylor', 'TT', '3339', 33,
15, 40, '7/10/2012', 'test',
'test', '64300337', 'Travis',
'Taylor', 'TT', '3339', '2/06/2013');
I ran this SQL command and received the following error-
"Error code -1, SQL state 21000: Scalar subquery is only allowed to return a single row.
Line 1, column 1"
I have ran this code successfully a few days ago. On top of that I have tried to manually enter in data in this table (using NetBeans) and have it auto generate the code, which resulted in the same error.
What is causing this error and how can I solve/bypass it?
One way in which you could run into this would be to do something like
CREATE FUNCTION F(...) ...
F((SELECT COL FROM T))
But you could instead write
... (SELECT F(COL) FROM T) provided the new context permits a subquery, that is.

Advantage Database 8.1 SQL IN clause

Using Advantage Database Server 8.1 I am having trouble executing a successful query. I am trying to do the following
SELECT * FROM Persons
WHERE LastName IN ('Hansen','Pettersen')
To check for multiple values in a column. But I get an error when I try to execute this query in Advantage.
Edit - Error
poQuery: Error 7200: AQE Error: State = 42000; NativeError = 2115; [iAnywhere Solutions][Advantage SQL Engine]Expected lexical element not found: ( There was a problem parsing the
WHERE clause in your SELECT statement. -- Location of error in the SQL statement is: 46
And here is the SQL i'm executing
select * from "Pat Visit" where
DIAG1 IN = ('43644', '43645', '43770', '43771', '43772', '43773', '43774',
'43842', '43843', '43845', '43846', '43847', '43848', '97804', '98961',
'98962', '99078')
Done
Does anyone have any Idea how I could do something similar in advantage that would be efficient as well?
Thanks
You have an extraneous = in the statement after the IN. It should be:
select * from "Pat Visit" where
DIAG1 IN ('43644', '43645', <snip> )