Buenas,
necesito crear una grip app con una query muy compleja que es la siguiente:
SELECT c.id AS idcc, c.nombreComercial AS cc_name, ag.id AS idag, ag.login AS ag_login, a.id AS idal, CONCAT(a.fecha, ’ ', a.hora) as dt_alarm, ar.dt_act1 AS dt_taken, ar.dt_act2 AS dt_tracking FROM cras c INNER JOIN agentes ag ON c.id=ag.id_cra INNER JOIN alarmas a ON c.id=a.id_cra AND ag.id=a.agente_id INNER JOIN (
SELECT nia1.id_alarma AS idal1, nia2.id_alarma AS idal2, nia1.dt as dt_act1, nia2.dt as dt_act2
FROM (
SELECT t1.id_alarma, CONCAT(t1.fecha, ' ', t1.hora) AS dt
FROM nectar_incidencias_alarma t1
WHERE t1.idaction=1
HAVING dt = (
SELECT MIN(CONCAT(t2.fecha, ' ', t2.hora)) FROM nectar_incidencias_alarma t2 WHERE t2.id_alarma=t1.id_alarma AND t1.idaction=1
)
) nia1
INNER JOIN
(
SELECT t3.id_alarma, CONCAT(t3.fecha, ' ', t3.hora) AS dt
FROM nectar_incidencias_alarma t3
WHERE t3.idaction=8
HAVING dt = (
SELECT MIN(CONCAT(t4.fecha, ' ', t4.hora)) FROM nectar_incidencias_alarma t4 WHERE t3.id_alarma=t4.id_alarma AND t4.idaction=8
)
) nia2
ON nia1.id_alarma=nia2.id_alarma
) ar
ON ar.idal1=a.id
ORDER BY cc_name, idal
Pero cuando ejecuto la grid me sale el siguiente error:
Error Se produjo un error al acceder a la base de datos
Unknown column ‘c.id’ in ‘where clause’
select count(*) from ( SELECT c.id AS idcc, c.nombreComercial AS cc_name, ag.id AS idag, ag.login AS ag_login, a.id AS idal, CONCAT(a.fecha, ’ ', a.hora) as dt_alarm, ar.dt_act1 AS dt_taken, ar.dt_act2 AS dt_tracking FROM cras c INNER JOIN agentes ag ON c.id=ag.id_cra INNER JOIN alarmas a ON c.id=a.id_cra AND ag.id=a.agente_id INNER JOIN ( SELECT nia1.id_alarma AS idal1, nia2.id_alarma AS idal2, nia1.dt as dt_act1, nia2.dt as dt_act2 FROM ( SELECT t1.id_alarma, CONCAT(t1.fecha, ’ ', t1.hora) AS dt FROM nectar_incidencias_alarma t1 WHERE t1.idaction=1 HAVING dt = ( SELECT MIN(CONCAT(t2.fecha, ’ ', t2.hora)) FROM nectar_incidencias_alarma t2 WHERE t2.id_alarma=t1.id_alarma AND t1.idaction=1 ) ) nia1 INNER JOIN ( SELECT t3.id_alarma, CONCAT(t3.fecha, ’ ', t3.hora) AS dt FROM nectar_incidencias_alarma t3 WHERE t3.idaction=8 HAVING dt = ( SELECT MIN(CONCAT(t4.fecha, ’ ', t4.hora)) FROM nectar_incidencias_alarma t4 WHERE t3.id_alarma=t4.id_alarma AND t4.idaction=8 ) ) nia2 ON nia1.id_alarma=nia2.id_alarma ) ar ON ar.idal1=a.id ) nm_sel_esp WHERE c.id=1
Es decir, no detecta bien la query y hace cosas raras provocando un error. La query original est? bien, se ejecuta perfectamente en mysql. El problema es que no la puedo encapsular en una vista porque mysql no permite crear vistas con subqueries, as? que lo ?nico que puedo usar es usarla directa. ?Alguien sabe como se soluciona?
Gracias,
Dani