Moodle Double Enrolling Students Using Cohorts - moodle

Are students enrolled a second time for a course if they are in a second COHORT? Or, is Moodle intelligent enough to SKIP them when the second COHORT is enrolled?

A student can be enrolled multiple times on the same course using different enrollment methods. But will only appear once in the list of students.
https://docs.moodle.org/311/en/Enrolments
Students are linked to the enrollment method rather than the course.
So if a course has 2 cohort enrollment methods and the student is in both cohorts, then they will have 2 enrollment records.
eg: The mdl_enrol table is the enrolment method
SELECT *
FROM mdl_user_enrolments ue
JOIN mdl_enrol e ON e.id = ue.enrolid
JOIN mdl_course c ON c.id = e.courseid
JOIN mdl_user u ON u.id = ue.userid
LIMIT 10

Related

Problem adding a number of students registered to a course (as a row) to a SELECT statement in POSTGRES

I have a database of courses. I need to get a name, a a topic, a teacher, a duration and a number of students registered. I get the first four successfully, but not the last one.
Here is what my tables look like:
courses table
journal table
all tables
That's the successful part for the first four:
SELECT c.name, t.topic_name AS topic, u.name || ' '|| u.surname AS TEACHER, ((c.end_date - c.start_date) / 7)::int AS duration
FROM public.course c
RIGHT JOIN public.topic t ON c.topic_id = t.topic_id
RIGHT JOIN public.teacher_course tc ON c.course_id = tc.course_id
RIGHT JOIN public.user u ON tc.teacher_id = u.user_id
WHERE u.role_id = 2;
Basically, to know the number of registered students per course, I only need to count records in the journal table for each course, but when I add
count(j.id_record) AS students_registered
it just breaks and asks me to group everything by and blah blah.
I'm confused about that. How to get this number correctly for each course?

Number of courses completed by student..MOODLE

what would be query to find no. of students who have completed their courses in MOODLE?
i am using follwing query :
elect mu.id as student_id,count(mcc.course) as completed_course from mdl_user mu join mdl_course_completions mcc on mcc.userid=mu.id JOIN mdl_course mc on mc.id=mcc.course WHERE mcc.userid = $user_id group by mu.id
I notice that you seem to have limited the results to only a single userid (WHERE mcc.userid = $user_id), so you should probably remove that restriction if you want to get details of more than one student.
You don't really need to join with the mdl_course or mdl_user tables, as there is only one mdl_course_completions table for each student + course combination.
You should, however, add a restriction on the 'timecompleted' field, to make sure it is not null (mdl_course_completions records are created when a student starts on a course, to record the timeenrolled and timestarted; when the course is complete the timecompleted field is set as well).
This should give you:
SELECT userid AS student_id, COUNT(*) AS completed_courses
FROM mdl_course_completions
WHERE timecompleted IS NOT NULL
Which will list the number courses each student has completed.
If, instead (and as stated at the start of the question), you want the number of students who have completed at least one course, then the query would be:
SELECT DISTINCT(userid)
FROM mdl_course_completions
WHERE timecompleted IS NOT NULL

Show field in MS Access query without including it in the group by clause

I'm working on a query that will eventually be used as the record source for a report.
I have a customers and orders table. I want to show customer_id, order_id, and order_date in a query, but I only want to show data associated with the earliest order date for each customer. Basically, I need to show the order_id field without including it in the group by clause. If I include it in the group by clause, I get a lot more records than I want. Based on my research, the code below will work in mysql, but not ms access.
Select customer.customer_id, order.order_id, min(order.order_dt)
From customer inner join order on customer.customer_id = order.customer_id
Group by customer.customer_id
I've tried grouping by order_id in a sub query and ordering by customer then date, then using the first function in the outer query. Unfortunately, the first function doesn't work as advertised.
Any help is greatly appreciated!
Does this work for you? It should bring up the earliest orders by order date for each customer. If there is more than one order on the earliest order date for a customer, all of those orders will be shown, though, so keep it in mind.
SELECT c.customer_id, o.order_id, o.order_dt
FROM customers AS c INNER JOIN (orders AS o INNER JOIN (SELECT customer_ID, MIN([order_dt]) AS MinOrder_dt FROM Orders GROUP BY customer_id) AS d ON (o.Customer_ID = d.customer_id) AND (o.[order_dt] = d.MinOrder_dt)) ON c.customer_id = o.customer_id;
I am deriving a table with just the customer_id and the min order_dt and joining customers and orders to that to only bring up the oldest orders.

Mysql Multiple Left join logic

Can someone explain multiple left join logic?
For example i have 3 tables: Company, company_text, company_rank.
Company has 4 records (id's: 1,2,3,4), company_text has 4 records with company names (1-a,2-b,3-c, 4-d), company_rank has following (1-1st, 2-2nd, 3-3rd). Note that company_rank table is not having 4th record. Now i want records of all companies using LEFT join. In case if there is no rank, display as 'zzzz' and sort in the descending order of rank and when rank is null sort by descending order of id.
select * from company
LEFT JOIN company_text ON company.id = company_text.id
LEFT JOIN company_rank ON company_text.id = company_rank.id
order by isnull(company_rank.rank,'zzzz'), rank desc
Will this work?
Basically i am trying to understand how LEFT JOIN works if there are many left joins? This doc. has good info on joins: http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html but it don't have information on how multiple LEFT JOINS work? In case if multiple joins are present, how records data will be pulled?

jpa avg(count(*))

Assum I have three tables User, Product and UserProduct. Once a user buys a product a record in the UserProduct table is created.
I would like to calculate an average number of products being bought by users. I can do this with plain SQL on mySql RDBMS like as follows:
select avg(c) from (select count(*) as c from user_product up join user u on (up.user_id = u.id) group by u.id) as res
Any chance I can do the same with JPA?
Update: I know that subqueries may be used in the WHERE or HAVING clause only, but still...