I am trying to query a database table with some filters. I have a form through which a user will create query attributes:
When user will click on search it will pass such values:
{camera_id: "9879"
, from_date: "2015/05/15"
, to_date: "2016/08/15"
, interval: "120"
, schedule: "{"Monday":["3:0-4:0"],"Tuesday":["1:30-3:30"]
,"Wed…":["0:0-3:0"],"Saturday":[],"Sunday":["1:0-3:0"]}"}
interval
has one of these names and values:
"All", "0"
"1 Frame Every 1 min", "1"
"1 Frame Every 5 min", "5"
...
"1 Frame Every hour", "60"
...
"1 Frame Every 24 hours", "1440"
For example: "1 Frame every 1 min"
. If 5 rows in snapshots
have been taken within 1 min, the query shall only return the first.
schedule
is being sent by using the FullCalendar
jquery plugin. Meaning:
Day: Monday, Time: 03:00 AM to 04:00 AM
Day: Tuesday, Time: 01:30 AM to 03:30 AM
...
Table snapshots
:
snapshot_id integer
, camera_id integer
, created_at timestamp
I want to select rows passing the filters. Temporal filters apply to created_at
. I want rows between from_date
and to_date
, considering the schedule
, and only get the first row per interval
.
Best Answer
A basic solution to retrieve only the first row per interval would be to
from_date
,to_date
andinterval
withgenerate_series()
.DISTINCT ON
to get the first per group.For simpler parameter handling I wrapped the query in a plpgsql function:
Call:
I ignored the additional filter
schedule
. Add that yourself. I would extract all intervals from the json document and add OR'edWHERE
conditions. Either use the SQLOVERLAPS
operator or the overlap operator&&
for range types. More:Also consider my answer to your previous question: