On 04/10/2013 10:31 AM, Chris Curvey
wrote:
Because I screwed up cutting and pasting to make an example. The unit_id in the output should, in fact, be distinct: unit_id | events | status_1_count ---------+--------+---------------- 1 | 50 | 34 2 | 27 | 18 3 | 50 | 34 4 | 2 | 0 You are correct, epoch is easy: abstime(epoch) or extract(epoch from timestamptz) depending on which direction you are going or for an hour difference just subtract 3600. The solution, however, misses the important complicating gotcha. The units I want listed are only those that have had at least one event in the last hour. But for each such unit, I only want the statistics to reflect the most-recent 50 events (even if those events occurred earlier than the current hour) provided the event occurred on the current date. So the events column can never be less than 1 nor more than 50. For example... One unit might have a single event at the start of the last hour but 49 more in the preceding 10 minutes. I want to see that unit and the stats for those 50 events. Same thing if a unit has 50 events clustered at the end of an hour - I don't want the earlier ones. Another might have 50 events early in the day but none this hour. I don't want to see that one. But I do want to see the one that had an event in the last hour late in the day along with the 48 other events that have accumulated since midnight. Cheers, Steve |