Combine two queries in PostgreSQL
I need help with concatenating in one array from two queries:
In a query related to the lack of student achievement in the quarter:
$sSqlAsistencia =
" SELECT
ca.idcadete,
coalesce(sum(i.cantidad),0) as cantidad
FROM
cadetes ca,
cursos c,
cursos_cadetes cc
left join inasistencias i on i.fk_idcurso = cc.fk_idcurso and i.fk_idcadete = cc.fk_idcadete
WHERE
c.habilitado = true
and ca.habilitado = true
and c.fk_idanolectivo = ".$aAnoLectivoBuscar."
and c.fk_idano = ".$aAnoBuscar."
and c.fk_iddivision = ".$aDivisionBuscar."
and cc.fk_idcurso = c.idcurso
and cc.fk_idcadete = ca.idcadete
and (EXTRACT(MONTH FROM i.fecha) in ".$trimestre ." or i.cantidad is null)
GROUP BY
ca.idcadete
";
$sSqlInasistencia = $oDB->Query($sSqlAsistencia);
idcadete | cantidad
203 | 4
305 | 0
120 | 10
Then the piece of code that resulted in me getting the student / cadet award for other requests:
$sSql = " SELECT idcadete, nombre, apellido, matricula
FROM cadetes
WHERE 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 to link the two consultations to the INNER JOIN, but don't shoot me wrong, as she will manage to combine both queries like this:
$premio = " SELECT a.idcadete, a.nombre, a.apellido, a.matricula, b.cantidad
FROM ".$rsCadetesConPremio."a inner join ".$sSqlInasistencia."b on a.idcadete = b.idcadete
ORDER BY a.idcadete";
---- MISTAKE -------
I need the following output: $ premio
idcadete | nombre | apellido | matricula| cantidad
203 | adrian | perez | 212121 | 4
+3
source to share
2 answers
Yes, you should be able to join any two arbitrary queries, wrapping the results as derived tables. However, you need to enclose both views in parentheses:FROM ( ...) a INNER JOIN ( ...) b
i.e:.
"SELECT a.idcadete, a.nombre, a.apellido, a.matricula, b.cantidad
FROM (".$rsCadetesConPremio.") a inner join (".$sSqlInasistencia.") b on a.idcadete = b.idcadete
ORDER BY a.idcadete"
Here's a simplified script .
0
source to share
$sSqlAsistencia = " SELECT ca.idcadete, ca.nombre, ca.apellido, ca.matricula coalesce(sum(i.cantidad),0) as cantidad FROM cadetes ca, cursos c, cursos_cadetes cc left join inasistencias i on i.fk_idcurso = cc.fk_idcurso and i.fk_idcadete = cc.fk_idcadete WHERE c.habilitado = true and ca.habilitado = true and c.fk_idanolectivo = ".$aAnoLectivoBuscar." and c.fk_idano = ".$aAnoBuscar." and c.fk_iddivision = ".$aDivisionBuscar." and cc.fk_idcurso = c.idcurso and cc.fk_idcadete = ca.idcadete and (EXTRACT(MONTH FROM i.fecha) in ".$trimestre ." or i.cantidad is null) and ca.idcadete in (" . $sSqlPromedioEnCadaMateria . " INTERSECT " . $sSqlPromedioConducta; if (strlen($aPromedioGeneralEdFisicaMayorABuscar)) { $sSqlAsistencia .= " INTERSECT " . $sSqlPromedioEnEdFisica; } $sSqlAsistencia .= ") GROUP BY ca.idcadete ";
0
source to share