Skip to content

Instantly share code, notes, and snippets.

@notsoluckycharm
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notsoluckycharm/56b414cd3c458a7355e8 to your computer and use it in GitHub Desktop.
Save notsoluckycharm/56b414cd3c458a7355e8 to your computer and use it in GitHub Desktop.
Retrieve Attendance Taken As a Percentage ( times taken / time should have taken ) For All Courses Taught At Schools Associated With A Specific Account For The Entire Year
select first_name, last_name, last_sign_in_at, days_taken, days_total, days_taken / days_total as performance
from(
SELECT tmp.user_id, sum( days_taken ) as days_taken, sum( days_total ) as days_total
FROM account_user
inner join account_institution on account_institution.account_id = account_user.account_id
inner join courses_institutions on courses_institutions.institution_id = account_institution.institution_id
left outer join (
SELECT courses_users.user_id, courses_users.course_id, count(*)::decimal as days_taken, courses_users.role,
(
SELECT count(*)::decimal
FROM generate_series ( '2014-08-25 17:58:40.957592'::timestamp, '2014-09-25 17:58:39.957696'::timestamp, interval '1d' ) h
WHERE EXTRACT(ISODOW FROM h) in ( select weekday from schedules s where s.active = true and s.course_id = courses_users.course_id )
) as days_total
FROM courses_users inner join school_calendars sc on sc.course_id = courses_users.course_id
WHERE (sc.start_at between '2014-08-25 17:58:40.958005' and '2014-09-25 17:58:39.958075' )
GROUP BY courses_users.user_id, courses_users.course_id, courses_users.role
) as tmp on ( tmp.course_id = courses_institutions.course_id AND tmp.user_id = account_user.user_id )
WHERE (account_user.account_id = 3) AND (tmp.role = 'Teacher')
group by tmp.user_id
) as tmp3 inner join users on tmp3.user_id = users.id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment