问题
Sorry for the basic question, but I am trying to run the following code with sqldf function in R, but every time it shows "Error in result_create(conn@ptr, statement) : near ')': syntax error" and I can't find exactly where the error is.
I tried to indent/format in different ways but the error indication to "near ')'" persists all the time.
It seems all parenthesis are fine.
Code:
sqldf("
SELECT Cia_2, Nombre_cia_2, N_orden_2, Tipo_orden_2, N_linea_2, N_direc_2, Nombre_alfa_2, Fecha_orden_2,
Surtido_programado_2, Original_prometida_2,
N_orden, Tipo_orden, N_linea, Cia,
Cant_recibida_2, Cant_pendiente_2, Cantidad_2, estado_sig_2, estado_ult_2,
(case when [N_orden_2] is null then 'SinOC' else 'ConOC' end) as TieneOC,
(case when [Cant_recibida_2]=0 and [Cant_pendiente_2]=0 and [estado_sig_2]=999 then 'Anulada'
else (case when [Cant_recibida_2] = [Cantidad_2] and [estado_sig_2] = 999 and [Tiene_OC_2] = 'SinOC' then 'Anulada' else 'NoAnulada' )) as Anulada,
(case when [N_direc_2] = 35999999 then 'Normal'
else (case when [N_direc_2] = 35999998 then 'Urgente'
else (case when [N_direc_2] = 35999997 then 'Emergente'
else (case when [N_direc_2] = 35999995 then 'Proyecto'
else (case when [N_direc_2] = 35999994 then 'Importaciones' else 'Expost' ))))) as TipoReq,
Unidad_negocios_2,
aprob_or_2.LastOfFecha_aprobac,
[SumOfOrden de Cambio_2]
FROM bases_or_con_oc
LEFT JOIN aprob_or_2 ON (N_orden = aprob_or_2.N_orden_OR)
AND (Tipo_orden = aprob_or_2.Tipo_ordenOR)
AND (N_linea = aprob_or_2.N_lineaOR)
WHERE estado_sig_2 > 110
AND (case when [Cant_recibida_2] = 0 and [Cant_pendiente_2] = 0 and [estado_sig_2] = 999 then 'Anulada'
else (case when [Cant_recibida_2] = [Cantidad_2] and [estado_sig_2] = 999 and [Tiene_OC_2] = 'SinOC'
then 'Anulada' else 'NoAnulada')) = 'noAnulada'
")
error image:
I also tried to exclude some lines in order to and find out the error point...
Do you know what's happening?
回答1:
You have several case without end each case must be a related end
sqldf("
SELECT Cia_2, Nombre_cia_2, N_orden_2, Tipo_orden_2, N_linea_2, N_direc_2, Nombre_alfa_2, Fecha_orden_2,
Surtido_programado_2, Original_prometida_2,
N_orden, Tipo_orden, N_linea, Cia,
Cant_recibida_2, Cant_pendiente_2, Cantidad_2, estado_sig_2, estado_ult_2,
(case when [N_orden_2] is null then 'SinOC' else 'ConOC' end) as TieneOC,
(case when [Cant_recibida_2]=0 and [Cant_pendiente_2]=0 and [estado_sig_2]=999 then 'Anulada'
else (case when [Cant_recibida_2] = [Cantidad_2] and [estado_sig_2] = 999 and [Tiene_OC_2] = 'SinOC'
then 'Anulada' else 'NoAnulada' END) END ) as Anulada,
(case when [N_direc_2] = 35999999 then 'Normal'
else (case when [N_direc_2] = 35999998 then 'Urgente'
else ( case when [N_direc_2] = 35999997 then 'Emergente'
else ( case when [N_direc_2] = 35999995 then 'Proyecto'
else (case when [N_direc_2] = 35999994 then 'Importaciones' else 'Expost' END ) END) END) END) END) as TipoReq,
Unidad_negocios_2,
aprob_or_2.LastOfFecha_aprobac,
[SumOfOrden de Cambio_2]
FROM bases_or_con_oc
LEFT JOIN aprob_or_2 ON (N_orden = aprob_or_2.N_orden_OR)
AND (Tipo_orden = aprob_or_2.Tipo_ordenOR)
AND (N_linea = aprob_or_2.N_lineaOR)
WHERE estado_sig_2 > 110
AND (case when [Cant_recibida_2] = 0 and [Cant_pendiente_2] = 0 and [estado_sig_2] = 999 then 'Anulada'
else ( case when [Cant_recibida_2] = [Cantidad_2] and [estado_sig_2] = 999 and [Tiene_OC_2] = 'SinOC'
then 'Anulada' else 'NoAnulada' END ) END) = 'noAnulada'
")
I hope I have not forgotten anyone
回答2:
Consider simplifying your SQL using the CTE clause, WITH(), supported by SQLite to avoid retyping same calculated column for Anulada in SELECT and WHERE clauses. And be sure to close all CASE statements with END:
WITH cte AS
(SELECT Cia_2, Nombre_cia_2, N_orden_2, Tipo_orden_2, N_linea_2, N_direc_2,
Nombre_alfa_2, Fecha_orden_2, Surtido_programado_2, Original_prometida_2,
N_orden, Tipo_orden, N_linea, Cia,
Cant_recibida_2, Cant_pENDiente_2, Cantidad_2, estado_sig_2, estado_ult_2,
CASE WHEN [N_orden_2] IS NULL THEN 'SinOC' ELSE 'ConOC' END AS TieneOC,
CASE
WHEN [Cant_recibida_2]=0 and [Cant_pENDiente_2]=0 AND [estado_sig_2] = 999
THEN 'Anulada'
ELSE
CASE
WHEN [Cant_recibida_2] = [Cantidad_2] AND [estado_sig_2] = 999 and [Tiene_OC_2] = 'SinOC'
THEN 'Anulada'
ELSE 'NoAnulada'
END
END AS Anulada,
CASE [N_direc_2]
WHEN 35999999 THEN 'Normal'
WHEN 35999998 THEN 'Urgente'
WHEN 35999997 THEN 'Emergente'
WHEN 35999995 THEN 'Proyecto'
WHEN 35999994 THEN 'Importaciones'
ELSE 'Expost'
END AS TipoReq,
Unidad_negocios_2, aprob_or_2.LastOfFecha_aprobac, [SumOfOrden de Cambio_2]
FROM bases_or_con_oc
LEFT JOIN aprob_or_2 ON (N_orden = aprob_or_2.N_orden_OR)
AND (Tipo_orden = aprob_or_2.Tipo_ordenOR)
AND (N_linea = aprob_or_2.N_lineaOR)
WHERE estado_sig_2 > 110
)
SELECT * FROM cte WHERE [Anulada] = 'NoAnulada';
来源:https://stackoverflow.com/questions/51972937/sqlite-in-r-error-in-result-createconnptr-statement-near