What I have: A single table with ids and prices for date and time.
What I want to do: To get the average difference of price for every id between the first and 15 minute of every day. Conditions: I also want to use the last available price in 15 minutes if the 15 is missing. For example if 15 is missing, use the price in 14 minutes.
What I did: My first step was to get the price after 15 minutes of every day for every id.
My code (I did a lot of tests with it, sorry if it doesn't look good and optimised enough) to get the last price(but not last available) in the 15 minutes interval:
WITH s as (
SELECT DATE(date_value) as dt, date_value, stock_id sid, open FROM stocks_1min
)
SELECT sid, dt, date_value, s.open as second_price
FROM s
INNER JOIN (
SELECT q.stock_id, DATE(q.date_value) as dat
FROM stocks_1min q
GROUP BY q.stock_id, dat
ORDER BY q.stock_id ASC
) as end_price
ON s.sid = end_price.stock_id
AND s.date_value = (SELECT date_value FROM s WHERE s.sid = end_price.stock_id AND s.dt = end_price.dat LIMIT 1) + interval '15 minutes'
ORDER BY sid, dt;
What I didn't do:
- get the last AVAILABLE price in that interval.
- get the first price in that interval and use it in the same query with the last price in the interval
- If I got both prices then I can use AVG and Group By to get the needed value.
Expected result (example): Table:
|stock_id|open(price)|date_value |
|1 |10 |01-12-2020 09:31|
|1 |12 |01-12-2020 09:46|
|1 |14 |02-12-2020 09:31|
|1 |20 |02-12-2020 09:46|
|2 |14 |01-12-2020 09:31|
|2 |16 |01-12-2020 09:46|
|2 |2 |02-12-2020 09:31|
|2 |16 |02-12-2020 09:46|
Result:
|stock_id|average_difference|
| 1 | 4 | ((12-10) + (20-14)) / 2 = 4
| 2 | 8 | ((16-14) + (16-2)) / 2 = 8
I have never used "Partition By", but maybe here is the place to start with it? It's my own project, where I want to test some statistical ideas for the stock market. Thanks in advance!
You can use this link where it's a sample for testing: http://sqlfiddle.com/#!17/83597/1