sql - Combine two queries in PostgreSQL -


i need union in single array of 2 queries :

in query related absences achievement of students in quarter :

$ssqlasistencia =          " select                 ca.idcadete,                                     coalesce(sum(i.cantidad),0) cantidad                               cadetes ca,                 cursos c,                 cursos_cadetes cc                 left join inasistencias on i.fk_idcurso = cc.fk_idcurso , i.fk_idcadete = cc.fk_idcadete                              c.habilitado = true                 , ca.habilitado = true                 , c.fk_idanolectivo = ".$aanolectivobuscar."                 , c.fk_idano = ".$aanobuscar."                 , c.fk_iddivision = ".$adivisionbuscar."                 , cc.fk_idcurso = c.idcurso                 , cc.fk_idcadete = ca.idcadete                 , (extract(month i.fecha)  in ".$trimestre ." or i.cantidad null)              group                 ca.idcadete              ";     $ssqlinasistencia = $odb->query($ssqlasistencia);  idcadete | cantidad  203      |    4 305      |    0 120      |    10  

then piece of code have result student / cadet award meeting other inquiries:

$ssql = " select idcadete, nombre, apellido, matricula          cadetes         idcadete in         (" . $ssqlpromedioencadamateria . " intersect " . $ssqlpromedioconducta;         if (strlen($apromediogeneraledfisicamayorabuscar)) {           $ssql .= " intersect " . $ssqlpromedioenedfisica;         }   $ssql .=    ")";     $rscadetesconpremio=$odb->query($ssql);  idcadete | nombre | apellido | matricula 203      | adrian | perez    | 212121 

try link 2 consultations inner join shoot me wrong , should manage combine both queries this:

$premio = " select a.idcadete, a.nombre, a.apellido, a.matricula, b.cantidad         ".$rscadetesconpremio."a inner join ".$ssqlinasistencia."b on a.idcadete = b.idcadete         order a.idcadete"; 

----error-------

i need following result: $premio

idcadete | nombre | apellido | matricula| cantidad 203      | adrian | perez    | 212121   |     4 

yes, should able join 2 arbitrary queries wrapping results of both derived tables. need enclose both derived tables in parenthesis : from ( ...) inner join ( ...) b

i.e.:

"select a.idcadete, a.nombre, a.apellido, a.matricula, b.cantidad         (".$rscadetesconpremio.") inner join (".$ssqlinasistencia.") b on a.idcadete = b.idcadete         order a.idcadete" 

here's simplified fiddle.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -