Skip to content
Advertisement

PostgreSQL: aggregate records by time interval

I would like to generate reports of GPS capturing rate by travel modes.

I have in a table modes the type of travel modes used by users.

So for example, the following is the sample data for travel mode by user 10 for different trips.

And then for each user and for each trip, user’s GPS traces are recorded in a table plt_distinct:

Like so, for the user given in sample data above, following are the sample GPS traces for a particular trip:

The given sample are traces by metro mode. For analysis purposes, I am interested in aggregating the GPS traces interval for each mode (especially metro as GPS is not available underground).

I make available, these tables and the sample data in this DB-fiddle.

The expected result is something like this:

Advertisement

Answer

You should use LAG() and Filter clause to achieve this:

Try This:

DEMO of Fiddle

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement