Assumption 1 : Every ticket_id
can have multiple entries and exits. The data you have provide has 2 entries for ticket_id
869. Hence, the ActiveQuery
results are subject to uniq
. Even when a person has multiple entries (or exits) before required time, they are counted only once.
Assumption 2 : In some cases, between 2 entries, an exit may not be captured, as demonstrated by the sample data. Some discrepancy in the result might creep in because of this.
You can use the following query to obtain all people who are inside at one particular point of time, say reqd_timestamp
:
@entered = Member.where("timestamp <= ? AND event_type = ?",
reqd_timestamp, 'entry')
.pluck(:ticket_id).uniq
@exited = Member.where("timestamp <= ? AND event_type = ?",
reqd_timestamp, 'exit')
.pluck(:ticket_id).uniq
@inside = (@entered - @exit).count
Please note, ticket_id 869
will be counted as being inside as of 2015-12-17 16:48:00
because he has an entry
with id 1739
prior to the timestamp. He actually may have exited before stipulated time and subsequently making another entry as id 1740
. But no data being available, that particular information cannot be captured.