oracle ora-00904 with symfony2 and doctrine2 - sql

I have a doctrine DQL that transforms into this SQL:
SELECT o0_.id AS ID0,
o0_.pmn AS PMN1,
o0_.neteo AS NETEO2,
sum(o1_.monto_total) AS SCLR3,
o1_.periodo AS PERIODO4,
o1_.pagado AS PAGADO5
FROM operador o0_
LEFT JOIN operador_hub o2_ ON o0_.id = o2_.operador_id
LEFT JOIN outcollect o1_ ON o0_.id = o1_.operador_id
WHERE o0_.ishub = 0
AND o0_.neteo = 1
AND o0_.incluirReportes = 1
AND o0_.pmn NOT IN ('CHLTM', 'CHLCM')
AND o0_.id NOT IN (SELECT o3_.id FROM operador o3_ LEFT JOIN operador_hub o4_ ON o3_.id = o4_.operador_id LEFT JOIN outcollect o5_ ON o3_.id = o5_.operador_id WHERE (o4_.desde <= o5_.periodo AND (o4_.hasta >= o5_.periodo OR o4_.hasta IS NULL)))
AND o0_.activo = 1
GROUP BY o0_.pmn, PERIODO4
ORDER BY o0_.pmn ASC
this gives an ORA-00904: "PERIODO4": identificador no válido
the error is directly related to the GROUP BY o0_.pmn, PERIODO4 line
It seems related to doctrine that cannot translate the query to an ORACLE syntax. I'm using OCI8 driver.
Any thoughts?
---- edit ----
here is the DQL:
SELECT partial operador.{id, pmn, neteo},
sum(outcollect.montoTotal) as montoOut,
outcollect.periodo as periodo,
outcollect.pagado as pagado
FROM RoamingOperadoresBundle:Operador operador
LEFT JOIN operador.operadorHub operadorHub
LEFT JOIN operador.outcollect outcollect
WHERE operador.ishub = 0
AND operador.neteo = 1
AND operador.incluirReportes = 1
AND operador.pmn not in('CHLTM', 'CHLCM')
AND operador.id not in(SELECT op.id
FROM RoamingOperadoresBundle:Operador op
LEFT JOIN op.operadorHub opHub
LEFT JOIN op.outcollect out
WHERE (opHub.desde <= out.periodo and (opHub.hasta >= out.periodo or opHub.hasta is null)))
AND operador.activo = 1
GROUP BY operador.pmn, periodo ORDER BY operador.pmn

I'm not sure how you are generating this table expression, but you are getting the error because you aren't including all non-aggregated columns in the GROUP BY clause.
Also note - you can't group by an alias. You must refer to the column by specifying it in table.column form.
It should look like this:
GROUP BY o0_.pmn, o0_.neteo, o1_.periodo, o1_.pagado

Related

SQL Multiple inner joins with max() for latest recorded entry

Attempting to build SQL with INNER JOIN's. The INNER JOIN's work ok, now I need to add the MAX() function for limiting the rows to just most recent. Added this INNER JOIN client_diagnosis_record ON SELECT cr.PATID, cr.date_of_diagnosis, cr.most_recent_diagnosis...
Received this SQL code error, need some help, I'm sure it a simple oversight but my eyes are getting dim from looking so long...
Syntax error: [SQLCODE: <-4>:
SQLCODE: <-4>:<A term expected, beginning with one of the following: identifier, constant, aggregate, %ALPHAUP, %EXACT, %MVR, %SQLSTRING, %
[%msg: < The SELECT list of the subquery
SELECT pd.patient_name,
cr.PATID,
cr.date_of_diagnosis,
cr.EPISODE_NUMBER,
ce.diagnosing_clinician_value,
ce.data_entry_user_name,
most_recent_diagnosis
FROM client_diagnosis_record cr
INNER JOIN patient_current_demographics pd ON cr.patid = pd.patid
INNER JOIN client_diagnosis_entry ce ON ce.patid = pd.patid
AND cr.ID = ce.DiagnosisRecord
INNER JOIN client_diagnosis_record ON (SELECT cr.PATID,
cr.date_of_diagnosis,
cr.most_recent_diagnosis
FROM ( SELECT patid,
date_of_diagnosis,
MAX(ID) AS most_recent_diagnosis
FROM client_diagnosis_record) cr
INNER JOIN RADplus_users ru ON ru.staff_member_id = ce.diagnosing_clinician_code
WHERE cr.PATID <> '1'
AND ce.diagnosis_status_value ='Active'
AND (ru.user_description LIKE '%SOA' OR ru.user_description LIKE '%OA')
GROUP BY cr.PATID
I tried to re-format you query and it seems your query syntax is not correct. You may try below query -
SELECT pd.patient_name,
cr.PATID,
cr.date_of_diagnosis,
cr.EPISODE_NUMBER,
ce.diagnosing_clinician_value,
ce.data_entry_user_name,
most_recent_diagnosis
FROM client_diagnosis_record cr
INNER JOIN (SELECT patid,
date_of_diagnosis,
MAX(ID) AS most_recent_diagnosis
FROM client_diagnosis_record
GROUP BY patid,
date_of_diagnosis) cr2 ON cr.PATID = cr2.PATID
AND cr.date_of_diagnosis = cr2.date_of_diagnosis
AND cr.ID = cr2.most_recent_diagnosis
INNER JOIN patient_current_demographics pd ON cr.patid = pd.patid
INNER JOIN client_diagnosis_entry ce ON ce.patid = pd.patid
AND cr.ID = ce.DiagnosisRecord
INNER JOIN RADplus_users ru ON ru.staff_member_id = ce.diagnosing_clinician_code
WHERE cr.PATID <> '1'
AND ce.diagnosis_status_value ='Active'
AND (ru.user_description LIKE '%SOA' OR ru.user_description LIKE '%OA')
GROUP BY cr.PATID

select top 1 in subquery?

I'm trying to get a top 1 returned for each code in this query but this is giving me syntax errors. Any ideas what I can do?
SELECT d.doc_no,
d.doc_short_name,
d.doc_name,
r.revision_code,
r.revision,
r.description,
dtg.group_name,
(SELECT top 1 di.index_user_id
FROM document_instance
WHERE doc_no = d.doc_no
ORDER BY entry_time DESC) AS 'last indexed by'
FROM documents d
LEFT JOIN document_revision r ON r.doc_no = d.doc_no
LEFT JOIN document_instance di ON di.doc_no = d.doc_no
LEFT JOIN document_type_group dtg ON dtg.doc_no = d.doc_no
Assuming Sybase ASE, the top # clause is only supported in derived tables, ie, it's not supported in correlated sub-queries like you're attempting.
Also note that order by is not supported in any sub-queries (derived table or correlated).
If I'm reading the query correctly, you want the index_user_id for the record with the max(entry_time); if this is correct, and assuming a single record is returned for a given doc_no/entry_time combo, you could try something like:
SELECT d.doc_no,
d.doc_short_name,
d.doc_name,
r.revision_code,
r.revision,
r.description,
dtg.group_name,
(SELECT d2.index_user_id
FROM document_instance d2
WHERE d2.doc_no = d1.doc_no
and d2.entry_time = (select max(d3.entry_time)
from document_instance d3
where d3.doc_no = d1.doc_no)
) as 'last indexed by'
FROM documents d
LEFT JOIN document_revision r ON r.doc_no = d.doc_no
LEFT JOIN document_instance d1 ON d1.doc_no = d.doc_no
LEFT JOIN document_type_group dtg ON dtg.doc_no = d.doc_no

Getting error: "Column ambiguously defined "

I am running the following query and getting column ambiguously defined :
SELECT
S.SUB_ID
,M.FLEETID
,M.TGID
,M.TGNO
,R.TGTYPE
,R.MODEID
,COUNT(1)
FROM
INF_SUBSCRIBER_ALL S
INNER JOIN
INF_TALKGROUP_MEMBER M ON S.SUB_ID = M.SUBID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.TGID = R.TGID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.FLEETID = R.FLEETID
WHERE
S.SUB_STATE = 'B01'
AND M.STATUS = 'M01'
GROUP BY
S.SUB_ID, M.FLEETID, M.TGID, M.TGNO, R.TGTYPE, R.MODEID;
You have used aliase-R twice, that is why the error,
You dont need to include a table twice into JOINs for adding another condition(M.FLEETID = R.FLEETID),
You can give the corresponding condition in the first occurce itselt using AND operator.
SELECT
S.SUB_ID
,M.FLEETID
,M.TGID
,M.TGNO
,R.TGTYPE
,R.MODEID
,COUNT(1)
FROM
INF_SUBSCRIBER_ALL S
INNER JOIN
INF_TALKGROUP_MEMBER M ON S.SUB_ID = M.SUBID
INNER JOIN
INF_TALKGROUP_MODE_RELATION R ON M.TGID = R.TGID AND M.FLEETID = R.FLEETID
WHERE
S.SUB_STATE = 'B01'
AND M.STATUS = 'M01'
GROUP BY
S.SUB_ID, M.FLEETID, M.TGID, M.TGNO, R.TGTYPE, R.MODEID;
Hope this helps.

oracle ora-00979 symfony2 doctrine2

I'm building the following DQL:
SELECT partial operador.{id, pmn, neteo},
sum(outcollect.montoTotal) as montoOut,
outcollect.periodo as periodo,
outcollect.pagado
FROM RoamingOperadoresBundle:Operador operador
LEFT JOIN operador.operadorHub operadorHub
LEFT JOIN operador.outcollect outcollect
WHERE operador.ishub = 0
AND operador.neteo = 1
AND operador.incluirReportes = 1
AND operador.pmn not in('CHLTM', 'CHLCM')
AND operador.id not in(SELECT op.id
FROM RoamingOperadoresBundle:Operador op
LEFT JOIN op.operadorHub opHub
LEFT JOIN op.outcollect out
WHERE (opHub.desde <= out.periodo and (opHub.hasta >= out.periodo or opHub.hasta is null))
)
AND operador.activo = 1
GROUP BY operador.pmn, outcollect.periodo ORDER BY operador.pmn
Which transforms into this SQL:
SELECT o0_.id AS ID0,
o0_.pmn AS PMN1,
o0_.neteo AS NETEO2,
sum(o1_.monto_total) AS SCLR3,
o1_.periodo AS PERIODO4,
o1_.pagado AS PAGADO5
FROM operador o0_
LEFT JOIN operador_hub o2_ ON o0_.id = o2_.operador_id
LEFT JOIN outcollect o1_ ON o0_.id = o1_.operador_id
WHERE o0_.ishub = 0
AND o0_.neteo = 1
AND o0_.incluirReportes = 1
AND o0_.pmn NOT IN ('CHLTM', 'CHLCM')
AND o0_.id NOT IN (SELECT o3_.id FROM operador o3_ LEFT JOIN operador_hub o4_ ON o3_.id = o4_.operador_id LEFT JOIN outcollect o5_ ON o3_.id = o5_.operador_id WHERE (o4_.desde <= o5_.periodo AND (o4_.hasta >= o5_.periodo OR o4_.hasta IS NULL)))
AND o0_.activo = 1
GROUP BY o0_.pmn, o1_.periodo
ORDER BY o0_.pmn ASC
Which gives the following error:
ORA-00979: no es una expresión GROUP BY
Any pointers on how to change the DQL to solve this problem?
I'm using doctrine2, symfony2 and OCI8
Per Oracle documentation
ORA-00979 not a GROUP BY expression
Cause: The GROUP BY clause does not contain all the expressions in the SELECT clause. SELECT expressions that are not included in a group
function, such as AVG, COUNT, MAX, MIN, SUM, STDDEV, or VARIANCE, must
be listed in the GROUP BY clause.
Action: Include in the GROUP BY clause all SELECT expressions that
are not group function arguments.
So you need to include all the expressions/column in select list to your group by. change the group by of your SQL query to below
group by o0_.pmn, o1_.periodo, o0_.neteo, o1_.pagado, o0_.id
GROUP BY of DQL to
GROUP BY operador.pmn, outcollect.periodo, operador.id,
operador.neteo, outcollect.pagado

Left Join on Same table - Not recognizing nested SELECT statement

I am trying to pull two different values based on different criteria from the same table and in my Left Join of the same table it is not recognizing the SELECT statement.
The error is as follows:
Dynamic SQL Error
SQL error code = -104
Token unknown - line 7, char -1
SELECT.
The SQL Statement:
SELECT
b.dept,b.typ,c.brand,c.style,c.ext,c.description,
max(c.price),max(c.last_cost),sum(c.quan) "TOTAL INV",D.QUAN "WEB INV"
FROM
invt c
left outer join (
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
FROM invt WHERE store in ('997')
group by dept,typ,brand,style,ext,description) d
on (b.store = d.store and b.style = d.style and b.brand = d.brand)
LEFT OUTER JOIN
sku b
on c.style = b.style and c.brand = b.brand
where c.quan <> 0 or c.ord <> 0
GROUP BY
b.dept,b.typ,c.brand,c.style,c.ext,c.description
Try changing this line:
SELECT dept,typ,brand,style,ext,description,sum(quan) as d.quan
to this:
SELECT store,dept,typ,brand,style,ext,description,sum(quan) as quan
You do not need the d alias here.
UPDATE:
As #Jeremy Holovacs mentioned, you also seem to be using d.store for your join but it does not exist in your subquery.