Foros del Web » Programando para Internet » PHP »

Consulta agrupando por varias condiciones

Estas en el tema de Consulta agrupando por varias condiciones en el foro de PHP en Foros del Web. No se si me explique con claridad veran tengo esta tabla "difuntos_totales" la cual posee estos campos: Código PHP: ` Difunto_ID `  int ( 10 ...
  #1 (permalink)  
Antiguo 14/08/2009, 07:58
Avatar de JuJoGuAl  
Fecha de Ingreso: julio-2009
Ubicación: Venezuela
Mensajes: 754
Antigüedad: 14 años, 8 meses
Puntos: 19
Consulta agrupando por varias condiciones

No se si me explique con claridad veran tengo esta tabla "difuntos_totales" la cual posee estos campos:
Código PHP:
`Difunto_IDint(10unsigned NOT NULL default '0',
  `
FMuertedate default NULL,
  `
FEnterramientodate default NULL,
  `
Nombre_Apellidovarchar(100) default NULL,
  `
Edadvarchar(15) default NULL,
  `
Inhumacionint(10unsigned default NULL,
  `
Defuncion_Localint(10unsigned default NULL,
  `
Parroquiavarchar(15) default NULL,
  `
Cod_Bovedaint(10unsigned default NULL,
  `
Tipo_Bovedavarchar(10) default NULL,
  `
Bloqueint(2unsigned default NULL,
  `
Gratisint(2unsigned default '0'
y tiene estos Registros basicamente:

Código PHP:
DIA-Tipo_Boveda
12
-Sencilla
12
-Sencilla
12
-Sencilla
12
-Sencilla
12
-Sencilla
12
-Sencilla
12
-Sencilla
12
-Doble
12
-Restos
12
-Infantil
12
-Sencilla
13
-Sencilla 
y mi codigo es:
Código PHP:
<table border="1" width="100%">
       <tr>
             <td rowspan="2" align="center" class="Reporte" width="5%"><font size="2"><b>Fecha</b></td>
             <td colspan="3" align="center" class="Reporte" width="5%"><font size="2"><b>Sencillas</b></td>
             <td colspan="3" align="center" class="Reporte" width="5%"><font size="2"><b>Dobles</b></td>
             <td colspan="3" align="center" class="Reporte" width="5%"><font size="2"><b>Infantiles</b></td>
             <td colspan="3" align="center" class="Reporte" width="5%"><font size="2"><b>Restos</b></td>
             <td colspan="3" align="center" class="Reporte" width="5%"><font size="2"><b>Fetos</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Ingreso</b></td>
         </tr>
         <tr>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Cantidad</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Costo</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Total</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Cantidad</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Costo</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Total</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Cantidad</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Costo</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Total</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Cantidad</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Costo</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Total</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Cantidad</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Costo</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Total</b></td>
             <td align="center" class="Reporte" width="5%"><font size="2"><b>Total:</b></td>
         </tr>
<?php
include("config.php");
$Boveda mysql_query("SELECT day(FMuerte) as dia, tipo_boveda FROM difuntos_totales");
$fecha;
$Sencilla=0;
$Doble=0;
$Infantil=0;
$Restos=0;
$Feto=0;
while (
$row=mysql_fetch_assoc($Boveda))
{
if (
$row["dia"]>0)
{
$fecha=$row["dia"];
}
if (
$row["tipo_boveda"]=="Sencilla")
{
$Sencilla=$Sencilla+1;
}
if (
$row["tipo_boveda"]=="Doble")
{
$Doble=$Doble+1;
}
if (
$row["tipo_boveda"]=="Infantil")
{
$Infantil=$Infantil+1;
}
if (
$row["tipo_boveda"]=="Restos")
{
$Restos=$Restos+1;
}
if (
$row["tipo_boveda"]=="Feto")
{
$Feto=$Feto+1;
}
    echo 
"<tr>
    <td class=\"Reporte\" align=center>$fecha</span></td>
    <td class=\"Reporte\" align=center>$Sencilla</span></td>
    <td class=\"Reporte\" align=center>&nbsp;</span></td>
    <td class=\"Reporte\" align=center>&nbsp;</span></td>
    <td class=\"Reporte\" align=center>$Doble</span></td>
    <td class=\"Reporte\" align=center>&nbsp;</span></td>
    <td class=\"Reporte\" align=center>&nbsp;</span></td>
    <td class=\"Reporte\" align=center>$Infantil</span></td>
    <td class=\"Reporte\" align=center>&nbsp;</span></td>
    <td class=\"Reporte\" align=center>&nbsp;</span></td>
    <td class=\"Reporte\" align=center>$Restos</span></td>
    <td class=\"Reporte\" align=center>&nbsp;</span></td>
    <td class=\"Reporte\" align=center>&nbsp;</span></td>
    <td class=\"Reporte\" align=center>$Feto</span></td>
    <td class=\"Reporte\" align=center>&nbsp;</span></td>
    <td class=\"Reporte\" align=center>&nbsp;</span></td>
    <td class=\"Reporte\" align=center>&nbsp;</span></td>"
    
;
        
$Sencilla=0;
    
$Doble=0;
    
$Infantil=0;
    
$Restos=0;
    
$Feto=0;
    
$fecha=0;
}
mysql_free_result($Boveda)
?>
</table>
y necesito que me arroje algo como:
Dia=12 SENCILLAS=8 / Dobles=1 / Restos=1 / Intanfil=1
Dia13 Sencillas=1


Todo eso en una tabla porque es un informe.... :S

Como Puedo hacer????
  #2 (permalink)  
Antiguo 14/08/2009, 08:08
 
Fecha de Ingreso: julio-2009
Ubicación: .mysql_error ( XD )
Mensajes: 554
Antigüedad: 14 años, 9 meses
Puntos: 13
Respuesta: Consulta agrupando por varias condiciones

super lindo tu sistema XDDD


con un count puedes agrupar las cantidades totales
creo que seria algo asi:

Código PHP:
SELECT dia,tipo_bovedacount( * ) FROM tusmuertitos GROUP BY dia 
nos cuentas!
  #3 (permalink)  
Antiguo 14/08/2009, 08:20
Avatar de JuJoGuAl  
Fecha de Ingreso: julio-2009
Ubicación: Venezuela
Mensajes: 754
Antigüedad: 14 años, 8 meses
Puntos: 19
es que contar no me sirve porque lo que hara es que me agrupara las bovedas por cuenta es decir algo como (sencilla=10, doble=1 etc tec)

Añado que aunque suene feo el sistema es para un cementerio :D y este es un reporte que ellos necesitan

necesitan ver las bovedas que usan por dia en el mes , es decir el dia 12 usamos X sencillas x Dobles x Infantiles etc....

Si hago esto:
"SELECT day(fmuerte) as dia, tipo_boveda,count(Tipo_Boveda) as Bov from difuntos_totales group by tipo_boveda,fmuerte"

obtengo esto:

12 - Doble - 1
12 - Infantil - 1
12 - Restos - 1
12 - Sencilla - 8
13 - Sencilla - 1

Y aun no es lo que necesito :S

Lo que necesito es llamar varias veces la columna "tipo_boveda" pero cada una con uno de los valores que tiene... es lo que necesito o parte... :S

Agrego e llegado a esta consulta
"select day(fmuerte) as dia, tipo_boveda="Sencilla" as Sencillas,tipo_boveda="Doble" as Dobles,tipo_boveda="Infantil" as Infantiles,tipo_boveda="Restos" as Restos from difuntos_totales"

y me muestra:

Dia - Sencillas - Dobles - Infantiles - Restos
12 - 1 - 0 - 0 - 0
13 - 1 - 0 - 0 - 0
12 - 0 - 0 - 1 - 0
12 - 0 - 0 - 0 - 1
12 - 0 - 1 - 0 - 0
12 - 1 - 0 - 0 - 0
12 - 1 - 0 - 0 - 0
12 - 1 - 0 - 0 - 0
12 - 1 - 0 - 0 - 0
12 - 1 - 0 - 0 - 0
12 - 1 - 0 - 0 - 0
12 - 1 - 0 - 0 - 0

si agrego el group by dia me reduce a 2 celdas pero no me acumula las bovedas....

Problema Solucionado:
La consulta fue="select day(fmuerte)as dia,sum(tipo_boveda="Sencilla")as Sencillas,sum(tipo_boveda="Doble")as Dobles,sum(tipo_boveda="Infantil")as Infantil,sum(tipo_boveda="Restos")as Restos,sum(tipo_boveda="Fetos")as Fetos from difuntos_totales group by dia"

y arrojo todo de forma correcta Gracias =)

Última edición por JuJoGuAl; 14/08/2009 a las 09:35
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:29.