I have a database table containing one-minute periods of Open, Close, High, Low, Volume values for a security. I'm using SQL Server 2017, but 2019 RC is an option.
I am trying to find an efficient SQL Server query that can aggregate these into 5-minute windows, where:
- Open = first Open value of the window
- Close = last Close value of the window
- High = max High value of the window
- Low = min Low value of the window
- Volume = avg Volume across the window
Ideally this query would account for gaps in the data, i.e. be based on date calculations rather than counting preceding / following rows.
For example say I have (here's 6 mins of data):
| Time | Open | Close | High | Low | Volume | |------------------|------|-------|------|-----|--------| | 2019-10-30 09:30 | 5 | 10 | 15 | 1 | 125000 | | 2019-10-30 09:31 | 10 | 15 | 20 | 5 | 100000 | | 2019-10-30 09:32 | 15 | 20 | 25 | 10 | 120000 | | 2019-10-30 09:33 | 20 | 25 | 30 | 15 | 10000 | | 2019-10-30 09:34 | 20 | 22 | 40 | 2 | 13122 | | 2019-10-30 09:35 | 22 | 30 | 35 | 4 | 15000 | Not factored in, since this would be the first row of the next 5-minute window
I am trying to write a query that would give me (here's the first example of the 5-minute aggregate):
| Time | Open | Close | High | Low | Volume | |------------------|------|-------|------|-----|---------| | 2019-10-30 09:30 | 5 | 30 | 40 | 1 | 50224.4 |
Any tips? Am banging my head against the wall with the OVER clause and its PARTITION / RANGE options