Groups: count members

Groups: count members

by Gemma Lesterhuis -
Number of replies: 5
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Hello, 

I am working on a sql query that shows me the numbers of students in a group, as well the groupsname,course id, coursefullname and groupsname. 


Sofar - based on the database - I get this query running. Though I have some issues with it. 

  1. For some reason it also shows the total number of students in a group counted on all course. 
  2. When I try to place it in Configurable report it will not work due to "colum mdl_course_categories.name" is invalid in the select list because it not contained in either an agregate function or the group by clause. 


To be fair... I think I am causing conflicts at one point. Though I am working on this for about 2hours now and this is the closes I got. 


Is there anyone who can help me out, or has some tips? By the way I am working on Moodle 3.0 

Thank

Gemma

====

SELECT DISTINCT 

course_cat.name as categorie,

c.idnumber AS cursuscode, 

c.fullname AS cursus,

g.name As groepnaam

,count(members.groupid) as students

FROM `mdl_course` AS c

inner JOIN mdl_context AS cx ON c.id = cx.instanceid AND cx.contextlevel = '50'

inner jOIN mdl_role_assignments AS ra ON cx.id = ra.contextid

INNER JOIN mdl_role AS r ON ra.roleid = r.id

left JOIN mdl_user AS u ON ra.userid = u.id

left JOIN mdl_groups AS g ON c.id = g.courseid

left JOIN mdl_groups_members AS members ON g.id = members.groupid

left JOIN mdl_course_categories as course_cat on course_cat.id = c.category

where ra.roleid = 5 and course_cat.name like '%2016%'

Group by u.id

Average of ratings: -
In reply to Gemma Lesterhuis

Re: Groups: count members issue with Windows

by Gemma Lesterhuis -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
It seems the below code does the trick when it comes to counting.


Though I hope someone can help me on this error I get at one of my Moodle sites: 

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


Code: 

Select count(members.groupid) as students,course_cat.name as categorie,c.idnumber as cursuscode,c.fullname as cursus, g.name as groepnaam

From prefix_course as c

inner JOIN prefix_context AS cx ON c.id = cx.instanceid AND cx.contextlevel = '50'

inner jOIN prefix_role_assignments AS ra ON cx.id = ra.contextid

INNER JOIN prefix_role AS r ON ra.roleid = r.id

JOIN prefix_user AS u ON ra.userid = u.id

JOIN prefix_groups AS g ON c.id = g.courseid

JOIN prefix_groups_members AS members ON g.id = members.groupid

JOIN prefix_course_categories as course_cat on course_cat.id = c.category

 

where ra.roleid = 5 and course_cat.name like '%2016%'


Group by members.groupid

Order by count(u.id) DESC

In reply to Gemma Lesterhuis

Re: Groups: count members issue with Windows

by Randy Thornton -
Picture of Documentation writers

Gemma,

When you use GROUP BY, you have to list all columns that are in the select statement also in the group by clause, unless they are aggregates like the COUNT() you are using.

https://dev.mysql.com/doc/refman/5.6/en/group-by-handling.html

So

GROUP BY members.groupid, course_cat.name, c.idnumber, c.fullname, g.name

or

GROUP BY members.groupid, categorie, cursuscode, cursus, groepnaam


should be legal. Not sure if that's what you want on the report or not, though, but that's the source of the sql error.


- Randy

In reply to Randy Thornton

Re: Groups: count members issue with Windows

by Gemma Lesterhuis -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Thank you Randy, in the end I figured it out - just forgot to report it in the post. 

In reply to Randy Thornton

Re: Groups: count members issue with Windows

by Gemma Lesterhuis -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

Thank you Randy, in the end I figured it out - just forgot to report it in the post. 

In reply to Gemma Lesterhuis

Re: Groups: count members issue with Windows

by Randy Thornton -
Picture of Documentation writers

No problem. Now the solution is out there anyway for the next time it is a mystery to someone.