Ver Mensaje Individual
  #1 (permalink)  
Antiguo 22/03/2018, 11:42
Avatar de adelazeroth
adelazeroth
 
Fecha de Ingreso: junio-2014
Mensajes: 31
Antigüedad: 9 años, 10 meses
Puntos: 1
Problemas con el count

Que tal gente. No soy muy versado en el tema de PHP y SQL en conjunto. Estuve revisando en muchos sitios diferentes como resolver este problema y terminé por recurrir nuevamente a ustedes.

Lo que estoy tratando de hacer es generar una consulta que me contabilice la cantidad de veces que un dato se repite en el campo RUT y está información se agrega junto a otros campos que son descargados a un archivo CSV. Todo funciona de maravilla excepto por el Count que, en lugar de descargar al archivo los totales del conteo baja, según creo yo, los totales y los repite por el numero de registros que la consulta descarga.

He aquí la pequeña consulta (Por favor no me regañen por usar where en lugar de JOIN para los cruces, pero por más que me pelee con el JOIN no hubo modo)


Código:
$csv_file=fopen("gestiones.csv","w");
$titulos= "IDMandante;Rut;AÑO;MES;DIA;Grupo;IDResponsable;Nombre Responsable;Lug_Contacto;LugDescripcion;Cod_Contacto;CodDescripcion;Cod_Excusa;ExDescripcion;\n";
fwrite($csv_file,$titulos);	 
$HOY= date("d")."-".date("m")."-".date("Y");
$Ma =date("d")+1;
$TOMORROW = $Ma."-".date("m")."-".date("Y");

$BajaGestiones= "SELECT COUNT(A.Rut) AS 'CuentaRUT', A.IDMandante, A.Registro, A.IdResponsable, A.Lug_Contacto, A.Cod_Contacto, A.Cod_Excusa, B.IdResponsable AS 'Responsable', B.Grupo, B.NomResponsable, C.idMandante, C.cod, C.Descripcion AS 'LugDescripcion', D.idMandante, D.cod, D.Descripcion AS 'ExDescripcion', E.idMandante, E.cod, E.Descripcion AS 'ContDescripcion', F.IdMandante 

FROM DeudoresOGestion AS A, Grupos AS B, Ext_LugContacto AS C, Ext_Excusa AS D, Ext_Contacto AS E, Mandante AS F 

WHERE 

(A.Registro >= '$HOY') AND (A.Registro < '$TOMORROW')

AND (A.IdResponsable = B.IdResponsable) AND (A.IDMandante = C.idMandante) AND (A.IDMandante = D.idMandante) AND (A.IDMandante = E.idMandante) AND (A.IDMandante = F.idMandante) AND (A.Lug_Contacto = C.cod) AND (A.Cod_Excusa= D.cod) AND (A.Cod_Contacto = E.cod)

AND (A.IDMandante = 227 OR A.IDMandante = 226 OR A.IDMandante = 225 OR A.IDMandante = 224 OR A.IDMandante = 223 OR A.IDMandante = 222 OR A.IDMandante = 221 OR A.IDMandante = 219 OR A.IDMandante = 218 OR A.IDMandante = 217 OR A.IDMandante = 216 OR A.IDMandante = 210 OR A.IDMandante = 202 OR A.IDMandante = 198 OR A.IDMandante = 196 OR A.IDMandante = 192 OR A.IDMandante = 191 OR A.IDMandante = 189 OR A.IDMandante = 181 OR A.IDMandante = 180 OR A.IDMandante = 177 OR A.IDMandante = 174 OR A.IDMandante = 173 OR A.IDMandante = 164 OR A.IDMandante = 161 OR A.IDMandante = 148 OR A.IDMandante = 141 OR A.IDMandante = 132 OR A.IDMandante = 118 OR A.IDMandante = 111 OR A.IDMandante = 109 OR A.IDMandante = 85 OR A.IDMandante = 84 OR A.IDMandante = 83 OR A.IDMandante = 82 OR A.IDMandante = 74) 

AND (C.Descripcion LIKE '%telefo%' OR C.Descripcion LIKE '%respues%' OR C.Descripcion LIKE '%Presenc%')

GROUP BY A.IDMandante, A.Registro, A.Registro, A.IdResponsable, A.Lug_Contacto, A.Cod_Contacto, A.Cod_Excusa, B.IdResponsable, B.Grupo, B.NomResponsable, C.idMandante, C.cod, C.Descripcion, D.idMandante, D.cod, D.Descripcion, E.idMandante, E.cod, E.Descripcion, F.IdMandante"; 

$genera=odbc_exec($conn,$BajaGestiones);
while ($aumenta=odbc_fetch_row	($genera))	
{

$IDM=odbc_result($genera,"IDMandante");
$Rut=odbc_result($genera,"CuentaRUT");
$Registro=odbc_result($genera,"Registro");
$Lug_Contacto=odbc_result($genera,"Lug_Contacto");
$Lug_Contacto_Desc=odbc_result($genera,"LugDescripcion");
$Cod_Contacto=odbc_result($genera,"Cod_Contacto");
$CodDescripcion=odbc_result($genera,"ContDescripcion");
$Cod_Excusa=odbc_result($genera,"Cod_Excusa");
$ExDescripcion=odbc_result($genera,"ExDescripcion");
$IdResponsable=odbc_result($genera,"Responsable");
$NomResp=odbc_result($genera,"NomResponsable");
$Grupo=odbc_result($genera,"Grupo");
 
$almacena= $IDM.";".$Rut.";".date('Y',strtotime($Registro)).";".date('m',strtotime($Registro)).";".date('d',strtotime($Registro)).";".$Grupo.";".$IdResponsable.";".$NomResp.";".$Lug_Contacto.";".$Lug_Contacto_Desc.";".$Cod_Contacto.";".$CodDescripcion.";".$Cod_Excusa.";".$ExDescripcion."\n";
fwrite($csv_file,$almacena);
}
fclose($csv_file);
__________________
No Sign, No responsability ;D