I have an attendance table that I’m trying to query in order to create a dataset that I can graph with. My table stores attendance information like so:
date | student_id | present | session |
-----------|------------|---------|---------|
2012-09-01 | 1 | 1 | AM |
2012-09-01 | 1 | 1 | PM |
2012-09-01 | 2 | 0 | AM |
2012-09-01 | 2 | 1 | PM |
2012-09-02 | 1 | 0 | AM |
2012-09-02 | 1 | 0 | PM |
2012-09-02 | 2 | 0 | AM |
2012-09-02 | 2 | 0 | PM |
I’m trying to end up with a dataset that has each unique date, along with the number of present marks (divided by two, eventually, for a kinda accurate representation of attendance for each day).
2012-09-01 | 3 (1.5)
2012-09-02 | 0 (0)
This is so far the closest I’ve got
AttendanceMark.group(:date).count(:conditions => "present = 1")
but this resulting hash doesn’t include the dates where there are no present marks at all. How would I go about achieving this?
If you are using 0 and 1 for present and absent respectively, you can construct a query like this
This shall return
You can later divide all the sums by two.