I am trying to calculate the results of two COUNT() subqueries in order to produce a completion percentage. In this case I am getting the total number of courses assigned to an entity then comparing that entity with the number of courses completed by a user, then I would like to get the total percentage of courses completed by the user.. I am able to get the count of courses from the first entity, and the number of completed courses from the second, but cannot find a way of putting them together. I have found examples online of how one would calculate a percentage in the SELECT of a mySQL query, but am unsure if it is possible to do the same thing in Drupal. Any opinion or advice is appreciated.
This is an example of the working mySQL query I am trying to "Drupalize".
SELECT *,
user_completed_courses_count * 100.00 / total_courses_count total
FROM (
SELECT program_data.*,
(SELECT COUNT(*)
FROM user_program__field_user user_program
LEFT JOIN user_program__field_courses_completed user_prog_courses_completed
ON user_prog_courses_completed.entity_id = user_program.entity_id
WHERE user_program.field_user_target_id = $USER_ID***inserted UId*** ) user_completed_courses_count,
(SELECT COUNT(*)
FROM program_field_data program_data
LEFT JOIN learning_path__field_courses lp_courses
ON program_data.id = lp_courses.entity_id
LEFT JOIN course_entity__field_archive course_archive
ON program_courses.field_courses_target_id = course_archive.entity_id lp_courses_count
FROM program_field_data program_data
) q