Skip to content

Instantly share code, notes, and snippets.

@seejee
Last active August 29, 2015 14:09
Show Gist options
  • Save seejee/9c3fe9c55a801cff3e0e to your computer and use it in GitHub Desktop.
Save seejee/9c3fe9c55a801cff3e0e to your computer and use it in GitHub Desktop.
Student.joins(:classrooms).where(classrooms: { school_id: [15975, 692, 296] }).each do |s|
s.student_settings[:scantron_username] ||= (s.username.length > 16 ? s.username[0..11] + (rand() * 9999).to_i.to_s.rjust(4, '0') : s.username)
end
SELECT DISTINCT ON(s.id)
cu.name as "Customer",
coalesce(d.name,'No District') as "District",
sc.name as "School",
c.name as "Classroom",
u.first_name || ' ' || u.last_name as "Teacher",
u.email as "Teacher Email",
s.username as "Student TTM Username",
scantron_username as "Student Scantron Username",
(CASE WHEN s.username = scantron_username THEN true ELSE false END) as "Username matches?",
s.first_name as "First Name",
s.last_name as "Last Name",
g.number as "Current Grade"
FROM
students s
join grade_levels g on g.id = s.grade_level_id
left join classroom_students cs on cs.student_id = s.id
left join classrooms c on cs.classroom_id = c.id
left join schools sc on sc.id = c.school_id
left outer join districts d on sc.district_id = d.id
left join customers cu on cu.id = sc.customer_id
left join classroom_teachings ct on ct.classroom_id = c.id
left join users u on u.id = ct.user_id
join (
SELECT
ss.thing_id as student_id,
(regexp_matches(ss.value, '[\w\d]+'))[1] as scantron_username
FROM
student_settings ss
WHERE
ss.var = 'scantron_username'
) ss on ss.student_id = s.id
WHERE
s.is_active
AND c.name not like 'Temp Upload%'
AND sc.id IN (
16537, 16376, 16144, 16127, 16150, 16135, 16129, 16138, 16133, 16139, 16125, 16128, 16118, 16136, 16140, 16142, 16308, 16311, 1796 --ID
4714, 4697, 4721, 4611, 4684, 4726, 4792, --TX
14644, 14633, 16848, 12814, 12368, 12366, 12416, 14620, 12550, 14846, 12145, 15038, 12512, 12481, 14615, 12365, 12376 --OK
15984, 14148, 15983, 15974, 15975, 15978, 13887 --UT
)
;
SELECT DISTINCT ON(s.id)
'73-7012-7820' as "Site ID",
(regexp_matches(ss.value, '[\w\d]+'))[1] as "Student ID",
s.first_name as "First Name",
s.last_name as "Last Name",
'' as "Middle Initial",
'' as "Date Of Birth",
'' as "Gender",
g.number as "Current Grade",
'' as "Ethnicity",
'' as "Citizenship",
'' as "LEP",
'' as "Migratory Child",
'' as "MEP",
'' as "SMEP",
'' as "Student With Disability",
'' as "Title I",
'' as "Meal Assist",
'' as "IEP",
'' as "Test Password",
'' as "Reading Start",
'' as "Math Start",
'' as "Science Start",
'' as "Language Start",
'' as "Enrollment Date",
'' as "Algebra Start",
'' as "Reading Foundation Start"
FROM
students s
join grade_levels g on g.id = s.grade_level_id
join student_settings ss on ss.thing_id = s.id AND ss.var = 'scantron_username'
join classroom_students cs on cs.student_id = s.id
join classrooms c on cs.classroom_id = c.id
join schools sc on sc.id = c.school_id
join customers cu on cu.id = s.customer_id
join states st on st.id = cu.state_id
WHERE
s.is_active
AND st.code IN ('UT')
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment