I have a single table which stores bandwidth usage on the network over a period of time. One column will contain the date time (primary key) and another column will record the bandwidth. Data is recorded every minute. We will have other columns recording other data at that moment in time.
If the user requests the data on 15 minute intervals (within a 24 hour period given start and end date), is it possible with a single query to get the data I require or would I have to write a stored procedure/cursor to do this? Users may then request 5 minute intervals data etc.
I will most likely be using Postgres but are there other NOSQL options which would be better?
Any ideas?
The CTE
tprovides data like your table might hold: one timestamptsper minute with abandwidthnumber. (You don’t need that part, you work with your table instead.)Here is a very similar solution for a very similar question – with detailed explanation how this particular aggregation works:
Here is a similar solution for a similar question concerning running sums – with detailed explanation and links for the various functions used:
Additional question in comment
Retrieves one un-aggregated sample per 15 minute interval – from the last available row in the window. This will be the 15th minute if the row is not missing. Crucial parts are
DISTINCT ONandORDER BY.More information about the used technique here: