I am using PostgreSQL 9.2.
I have a table containing the time when some devices fail.
+----------+----------+---------------------+ | event_id | device | time | +----------+----------+---------------------+ | 1 | Switch4 | 2013-09-01 00:01:00 | | 2 | Switch1 | 2013-09-01 00:02:30 | | 3 | Switch10 | 2013-09-01 00:02:40 | | 4 | Switch51 | 2013-09-01 03:05:00 | | 5 | Switch49 | 2013-09-02 13:00:00 | | 6 | Switch28 | 2013-09-02 13:01:00 | | 7 | Switch9 | 2013-09-02 13:02:00 | +----------+----------+---------------------+
I want the rows to be grouped with a time difference of +/- 3 minutes, for example:
+----------+----------+---------------------+--------+ | event_id | device | time | group | +----------+----------+---------------------+--------+ | 1 | Switch4 | 2013-09-01 00:01:00 | 1 | | 2 | Switch1 | 2013-09-01 00:02:30 | 1 | | 3 | Switch10 | 2013-09-01 00:02:40 | 1 | | 4 | Switch51 | 2013-09-01 03:05:00 | 2 | | 5 | Switch49 | 2013-09-02 13:00:00 | 3 | | 6 | Switch28 | 2013-09-02 13:01:00 | 3 | | 7 | Switch9 | 2013-09-02 13:02:00 | 3 | +----------+----------+---------------------+--------+
I tried to do this using the window function, but in the section
[RANGE | ROWS] BETWEEN frame_start AND frame_end, where frame_start and frame_end may be one of the UNEQUIPPED SUGGESTED value, CURRENT CURRENT ROW value, NEXT UNLIMITED NEXT,
the value must be an integer expression that does not contain any variables, aggregate functions or window functions
So, given this, I cannot specify a time interval. Now I doubt that the window function can solve my problem. could you help me?
source share