Created
November 19, 2012 16:45
-
-
Save kalupa/4111761 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sql = "SELECT courses.id as 'course_id', | |
courses.name as 'name', | |
courses.likes as 'likes', | |
courses.duration as 'time_total', | |
completions.finished as 'completed', | |
completions.finished_at as 'completed_on', | |
COALESCE(completions.duration,0) as 'time_remaining', | |
essentials.id as 'essential_id', | |
electives.id as 'elective_id', | |
electives.points as 'total_points', | |
CASE | |
WHEN completions.finished THEN electives.points | |
WHEN COALESCE(completions.duration, 0) = 0 THEN 0 | |
ELSE NULL | |
END AS 'current_points', | |
COALESCE(completions.duration / courses.duration, 0) as 'percent_complete' | |
FROM courses | |
LEFT JOIN completions ON courses.id = completions.completable_id | |
AND completions.completable_type = 'Course' | |
AND completions.user_id = #{user.id} | |
LEFT JOIN electives ON electives.course_id = courses.id and electives.certification_id = #{cert_id} | |
LEFT JOIN essentials ON essentials.course_id = courses.id | |
LEFT JOIN terms ON terms.certification_id = #{cert_id} and essentials.term_id = terms.id | |
WHERE courses.id IN (#{course_ids.join(",")}) | |
AND COALESCE(essentials.term_id,0) = COALESCE(terms.id,0) | |
" | |
results = Course.connection.select_all(sql) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment