Moodle SQL assignment result/status - sql

I got this SQL code from Moodle SQL scripts How to make the result to include all itemnames whether it has a score or not? prefix_grade_grade table contains the score
prefix_grade_items table has the itemnames
Not sure what vendor, these scripts are used in Moodle LMS open source system
SELECT u.firstname AS 'Name' , u.lastname AS 'Surname', c.fullname AS 'Course', cc.name AS 'Category',
CASE WHEN gi.itemtype = 'Course'
THEN c.fullname + ' Course Total'
ELSE gi.itemname
END AS 'Item Name', ROUND(gg.finalgrade / gg.rawgrademax * 100 ,2) AS Percentage
FROM prefix_course AS c
JOIN prefix_context AS ctx ON c.id = ctx.instanceid
JOIN prefix_role_assignments AS ra ON ra.contextid = ctx.id
JOIN prefix_user AS u ON u.id = ra.userid
JOIN prefix_grade_grades AS gg ON gg.userid = u.id
JOIN prefix_grade_items AS gi ON gi.id = gg.itemid
JOIN prefix_course_categories AS cc ON cc.id = c.category
WHERE gi.courseid = c.id AND gi.itemname != 'Attendance' AND gi.itemmodule != 'scorm'
ORDER BY `Name` ASC

You could probably just switch the JOIN around for the grade_grades and grade_items and then do a LEFT JOIN on the grade_grades. I would also move the WHERE condition to the gi JOIN:
JOIN prefix_grade_items AS gi ON gi.courseid = c.id AND gi.itemname != 'Attendance' AND gi.itemmodule != 'scorm'
LEFT JOIN prefix_grade_grades AS gg ON gg.userid = u.id AND gg.itemid = gi.id
Moodle can use Mysql, Postgresql, MariaDB, SQL Server and Oracle - so the queries are generic. There are some functions available for cross database compatible queries here : https://docs.moodle.org/dev/Data_manipulation_API#SQL_compatibility_functions

Related

Get all data for an entity from another table in one query

I have this query
select *
from (
select c.*,p.name as project_name,u.firstname || ' ' || u.lastname as fullname, u.email as owner_email, u.payment_method, u as user, u.id as user_id, u.api_id, u.api_key,
v.name as vendor_name, v.exid as vendor_id, s.number as sim_number, vm.exid as vendor_model_id, vm.name as vendor_model_name, cr.status as is_recording,
cr.storage_duration as cloud_recording_storage_duration, cr.schedule as schedule, cr.frequency as frequency,
(select count(id) as total from camera_shares cs where c.id=cs.camera_id) as total_share
from cameras c
inner JOIN users u on c.owner_id = u.id
left JOIN projects p on c.project_id = p.id
left JOIN sims s on c.id = s.camera_id
left JOIN vendor_models vm on c.model_id = vm.id
left JOIN vendors v on vm.vendor_id = v.id
left JOIN cloud_recordings cr on c.id = cr.camera_id
) c
this gives me all cameras and with all relevant values which I require.
now there is another table, snapshot_extractors and it has a relation with the camera on id and camera_id in extractors table, as one camera can have more than 1 extractors.
In the above query, I want to get all extraction for one camera, I can do it in a separate query, but is it possible to get all extractions in the above query as an array of all extractions for a camera?
You can use another correlated subquery:
(select array_agg(e.extraction)
from snapshot_extractors e
where e.camera_id = c.camera_id
)

Problem with where clause and count transactions?

I want to count how many transactions I have by Currency. When I count without a where clause I get 0 transactions where I have NULL values but when I use a where clause with an IN operator I get a filtered result and no zero results. How to show 0 in count transactions?
SELECT
c.ShortName,
count(ad.AccountId) as No_of_transactions
FROM Currency c
LEFT JOIN Account a ON c.id = a.CurrencyId
LEFT JOIN AccountDetails ad ON a.id = ad.AccountId
LEFT JOIN [Location] l ON ad.LocationId = l.Id
LEFT JOIN LocationType lt ON l.LocationTypeId = lt.Id
WHERE lt.Name IN('Region Branch', 'City Branch')
GROUP BY c.ShortName
This is the result that I want to get:
EUR 31,
USD 0,
GBR 0
You have a LEFT JOIN. The filtering needs to go in the ON clause:
SELECT c.ShortName,
COUNT(lt.id) as No_of_transactions
FROM Currency c LEFT JOIN
Account a
ON c.id = a.CurrencyId LEFT JOIN
AccountDetails ad
ON a.id = ad.AccountId LEFT JOIN
[Location] l
ON ad.LocationId = l.Id LEFT JOIN
LocationType lt
ON l.LocationTypeId = lt.Id AND
lt.Name IN ('Region Branch', 'City Branch')
GROUP BY c.ShortName;
In your version, the non-matches turn into NULLs, which the WHERE conditions filter out.
You want to move the condition on the left joined table from the where clause to the on clause of the relevant join to avoid records being filtered out when they do not exist in that table.
Also, I think that you need COUNT(DISTINCT ad.id) (as per the comments, a transaction is uniquely represented by this column):
SELECT
c.ShortName,
COUNT(DISTINCT ad.id) as No_of_transactions
FROM Currency c
LEFT JOIN Account a ON c.id = a.CurrencyId
LEFT JOIN AccountDetails ad ON a.id = ad.AccountId
LEFT JOIN [Location] l ON ad.LocationId = l.Id
LEFT JOIN LocationType lt ON l.LocationTypeId = lt.Id AND lt.Name IN('Region Branch', 'City Branch')
GROUP BY c.ShortName
SELECT
c.ShortName,
COUNT(t.Id) as No_of_transations
FROM Currency c
LEFT JOIN Account a ON c.id = a.CurrencyId
LEFT JOIN AccountDetails ad ON a.id = ad.AccountId
LEFT JOIN [Location] l ON ad.LocationId = l.Id
LEFT JOIN (SELECT * FROM LocationType lt WHERE lt.Name IN('Region Branch', 'City
Branch')) AS t ON l.LocationTypeId = t.Id
GROUP BY c.ShortName

how to select some fields from tables in CakePHP

I tried to make query in cakephp with joins, but i want to get more fileds,
Here the query sql :
SELECT
sd.debut,
sd.fin,
fr.id,
f.id as formtaion_id,
s.id as seance_id,
fr.nom as formateur,
f.nom as formation,
r.title,
f.module,
sd.module as partie ,
f.couleur
FROM seances s
INNER JOIN formations f on s.formation_id = f.id
INNER JOIN seances_dates sd on s.id = sd.seance_id
INNER JOIN salles sa on sa.id = s.salle_id
INNER JOIN regions r on r.id = sa.region_id
INNER JOIN presence_formateurs pf ON pf.seance_id = s.id
INNER JOIN formateurs fr ON fr.id = pf.formateur_id
WHERE fr.archived = 0
AND fr.deleted is null
AND (
(sd.debut between '".$from."' and '".$to."')
OR
(sd.fin between '".$from."' and '".$to."')
)
GROUP BY sd.id
ORDER BY sd.debut
please help me to make that query in cakephp :
ClassRegistry::init('seance')->find('all'....
You should read very carefully
1. How to Retrieve data in CakePHP and
2. How to make Relation with different table

Moodle SCORM SQL

What table and field I need to use in SQL to find SCORM activities that reports back score to Moodle and exclude the SCORM objects that are set as non scoring. Basically looking for the SCORM settings field: Require Minimum Score in SQL
The query:
SELECT
CONCAT(u.firstname,' ',u.lastname ) AS 'fullname',
cc.name AS 'Category',
c.fullname AS 'Course',
gi.itemname AS 'Item Name',
ROUND(gg.finalgrade / gg.rawgrademax * 100 ,2) AS 'Percentage',
asgm.status as 'Status',
p.status AS 'Completed',
gi.itemmodule as 'Type',
s.completionscorerequired AS require_minimum_score
FROM prefix_course AS c
JOIN prefix_course_categories AS cc ON cc.id = c.category
JOIN prefix_context AS ctx ON c.id = ctx.instanceid
JOIN prefix_role_assignments AS ra ON ra.contextid = ctx.id
JOIN prefix_user AS u ON u.id = ra.userid
JOIN prefix_grade_items AS gi ON gi.courseid = c.id
JOIN prefix_scorm AS s ON c.id = s.course
LEFT JOIN prefix_grade_grades AS gg ON gi.id = gg.itemid AND gg.userid = u.id
LEFT JOIN prefix_course_completions AS p ON p.userid = u.id AND p.course = c.id
LEFT JOIN (
SELECT asgm.status as status, c.id as cid, u.id as uid, gi.id as giid
FROM prefix_course AS c
JOIN prefix_course_categories AS cc ON cc.id = c.category
JOIN prefix_context AS ctx ON c.id = ctx.instanceid
JOIN prefix_role_assignments AS ra ON ra.contextid = ctx.id
JOIN prefix_user AS u ON u.id = ra.userid
JOIN prefix_grade_items AS gi ON gi.courseid = c.id
JOIN prefix_assign AS asg ON gi.iteminstance = asg.id and asg.course = c.id
JOIN prefix_assign_submission AS asgm ON asg.id = asgm.assignment and asgm.userid = u.id
) AS asgm ON c.id = asgm.cid and u.id = asgm.uid and gi.id = asgm.giid
WHERE (gi.itemmodule = 'quiz' OR gi.itemmodule= 'assign' OR gi.itemmodule= 'scorm') AND c.visible=1
SELECT s.id AS scormid, s.name AS scormname,
s.completionscorerequired AS require_minimum_score,
c.id AS courseid, c.fullname AS coursename
FROM mdl_scorm s
JOIN mdl_course c ON c.id = s.course

sql query sum bringing back different results

I have the following two queries below, the Total is coming back different, but I am adding the sums in each of the query the same way. Why is the total coming back different?
select [Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants)),
County = co.Description
from ClassroomDemographics as demo
inner join Classrooms as c on demo.Classroom_Id = c.Id
inner join Sites as s on c.Site_Id = s.Id
inner join Profiles as p on s.Profile_Id = p.Id
inner join Dictionary.Counties as co on p.County_Id = co.Id
where co.Description = 'MyCounty'
Group By co.Description
select [Number Of DLL Children] = SUM(cd.NumberOfLanguageSpeakers),
[Total Children] = (SUM(demo.NumberOfPreschoolers) + SUM(demo.NumberOfToddlers) + SUM(demo.NumberOfInfants)),
County = co.Description
from ClassroomDLL as cd
inner join Classrooms as c on cd.Classroom_Id = c.Id
inner join Sites as s on c.Site_Id = s.Id
inner join Profiles as p on s.Profile_Id = p.Id
inner join Dictionary.Counties as co on p.County_Id = co.Id
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
where co.Description = 'MyCounty'
Group by co.Description
Just a quick glance over the two querties, I would presume that:
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
in the second query is excluding results that are in the first query, therefor the aggregated values will be different.
Your join to the Classrooms table is joining with an extra table in the 2nd query.
Query 1:
from ClassroomDemographics as demo
inner join Classrooms as c on demo.Classroom_Id = c.Id
Query 2:
from ClassroomDLL as cd
inner join Classrooms as c on cd.Classroom_Id = c.Id
...
inner join ClassroomDemographics as demo on c.Id = demo.Classroom_Id
My bet is that the ClassroomDLL table has less data in it, or has rows with a null for one of the join criteria columns, either of which could exclude rows from the results and throw your aggregate totals off.