Rails select, group, having, count -
i want count of how many appointments under 1 hr, 1 2 hrs , on 2 hours.
appointment .select("id, extract(minute (check_out_at - check_in_at)) minutes") .group("id, minutes") .having("count('minutes') < 60") .count
the count method return following exception:
activerecord::statementinvalid: pg::syntaxerror: error: syntax error @ or near "as" line 1: ...extract(minute (check_out_at - check_in_at)) minutes...
assuming id
primary key, neither need group by
nor having
. can count of appointments under 1 hour:
appointment.where("(extract(epoch (check_out_at - check_in_at))) < 3600").count
to count of appointments grouped hour, can this:
appointment.group("floor(extract(epoch (check_out_at - check_in_at))/3600)").count
the output like:
{0.0=>1, 2.0=>31, 3.0=>11}
meaning 1 appointment between 0-1 hour, 31 between 2-3 hours , on.
Comments
Post a Comment