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)
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.