0

Tengo una consulta la cual trae información de los alumnos, pero resulta que algunos están duplicados entonces pensé en agruparlos por la matricula (PEOPLE_ID) pero cuando coloco el group by me marca el siguiente error.

¿Podrían ayudarme a saber porque me esta mandando ese error?

Consulta sin group by

$this->db_sqlserver->select('p.PEOPLE_ID AS matricula, p.FIRST_NAME, p.MIDDLE_NAME, p.LAST_NAME AS a_paterno, p.Last_Name_Prefix AS a_materno, e.EmailType, e.Email AS email_institucional, a.ENROLL_SEPARATION AS estatus');        
$this->db_sqlserver->from('PEOPLE AS p');
$this->db_sqlserver->join('PEOPLETYPE AS pt', 'pt.PEOPLE_ID = p.PEOPLE_ID');
$this->db_sqlserver->join('EmailAddress AS e', 'e.PeopleOrgId = p.PEOPLE_ID');
$this->db_sqlserver->join('ACADEMIC AS a', 'a.PEOPLE_ID = p.PEOPLE_ID');
$this->db_sqlserver->where('pt.PEOPLE_TYPE', 'STUD');
$this->db_sqlserver->where('a.ACADEMIC_YEAR', date("Y"));
$this->db_sqlserver->where('a.ACADEMIC_TERM', "1");
$this->db_sqlserver->where('e.EmailType', 'INST');
$this->db_sqlserver->limit(50);  

$query = $this->db_sqlserver->get()->result();

return $query;

Resultado (usuarios duplicados)

introducir la descripción de la imagen aquí

Consulta con group by

$this->db_sqlserver->select('p.PEOPLE_ID AS matricula, p.FIRST_NAME, p.MIDDLE_NAME, p.LAST_NAME AS a_paterno, p.Last_Name_Prefix AS a_materno, e.EmailType, e.Email AS email_institucional, a.ENROLL_SEPARATION AS estatus');        
$this->db_sqlserver->from('PEOPLE AS p');
$this->db_sqlserver->join('PEOPLETYPE AS pt', 'pt.PEOPLE_ID = p.PEOPLE_ID');
$this->db_sqlserver->join('EmailAddress AS e', 'e.PeopleOrgId = p.PEOPLE_ID');
$this->db_sqlserver->join('ACADEMIC AS a', 'a.PEOPLE_ID = p.PEOPLE_ID');
$this->db_sqlserver->where('pt.PEOPLE_TYPE', 'STUD');
$this->db_sqlserver->where('a.ACADEMIC_YEAR', date("Y"));
$this->db_sqlserver->where('a.ACADEMIC_TERM', "1");
$this->db_sqlserver->where('e.EmailType', 'INST');
$this->db_sqlserver->group_by('p.PEOPLE_ID');
$this->db_sqlserver->limit(50);  

$query = $this->db_sqlserver->get()->result();

return $query;

Resultado (error)

Error Number: 42000/8120

[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Column 'PEOPLE.FIRST_NAME' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

SELECT "p"."PEOPLE_ID" AS "matricula", "p"."FIRST_NAME", "p"."MIDDLE_NAME", "p"."LAST_NAME" AS "a_paterno", "p"."Last_Name_Prefix" AS "a_materno", "e"."EmailType", "e"."Email" AS "email_institucional", "a"."ENROLL_SEPARATION" AS "estatus" FROM "PEOPLE" AS "p" JOIN "PEOPLETYPE" AS "pt" ON "pt"."PEOPLE_ID" = "p"."PEOPLE_ID" JOIN "EmailAddress" AS "e" ON "e"."PeopleOrgId" = "p"."PEOPLE_ID" JOIN "ACADEMIC" AS "a" ON "a"."PEOPLE_ID" = "p"."PEOPLE_ID" WHERE "pt"."PEOPLE_TYPE" = 'STUD' AND "a"."ACADEMIC_YEAR" = '2020' AND "a"."ACADEMIC_TERM" = '1' AND "e"."EmailType" = 'INST' GROUP BY "p"."PEOPLE_ID" ORDER BY 1 OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY

Filename: C:/xampp/htdocs/IntranetUCQ/system/database/DB_driver.php

Line Number: 691

Saludos y bonito viernes.

1 respuesta 1

1

Si se agrupa por una columna se debe aplicar una función de agregación al resto de columnas.

SELECT p.people_id AS matricula, -- columna agrupada
       Max(p.first_name), -- seleccionar el MAX() de ese grupo
       Max(p.last_name) as a_paterno -- seleccionar el MAX() de ese grupo
FROM   people AS p 
GROUP  BY people_id 

Todas las columnas del select que no son parte del group by deben estar en una función de agregación.

Forma 2

Se puede usar distinct para quitar los duplicados.

SELECT distinct p.people_id AS matricula, 
           p.first_name, 
           p.middle_name
    FROM   people AS p 
5
  • entonces le pongo un max() a cada uno de los datos que quiero traer en la consulta?
    – Javier fr
    Commented el 6 nov. 2020 a las 15:08
  • Sí, eso sería usado group by pero también se puede usar distinct actualicé la respuesta.
    – Lobos
    Commented el 6 nov. 2020 a las 15:11
  • les estoy poniendo el max a los datos que quiero traer, pero tengo una duda. el max a los datos que tienen alias ejemplo p.LAST_NAME AS a_paterno, se encierra todo el dado asi max(p.LAST_NAME AS a_paterno) o solo el alias asi p.LAST_NAME AS max(a_paterno)?
    – Javier fr
    Commented el 6 nov. 2020 a las 15:17
  • MAX(p.LAST_NAME) as a_paterno Se encierra la columna el alias queda afuera.
    – Lobos
    Commented el 6 nov. 2020 a las 15:19
  • 1
    Muchas gracias!! @Lobos
    – Javier fr
    Commented el 6 nov. 2020 a las 15:20

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.