I have a database table results with a number of columns, showing a running count of how many goals have been scored at each minute.
e.g.
f_total_ftg # full time goals
f_total_htg # half time goals
f_total_1mg # 1 minute goals
Each insert in the database has a column f_datetime, which is the associated timestamp.
I'm trying to get an average of each goal column and then take the overall avg and last 2 weeks average and divide by 2.
Example:
f_avg_total_ftg_overall = 3.12
f_avg_total_ftg_last_2_weeks = 2.42
f_avg_ftg = (f_avg_total_ftg_overall + f_avg_total_ftg_last_2_weeks) / 2
My current solution is to take each column overall/last 2 weeks separately, returning a dict in my python code where I do the end calculation, but this should be doable in one query I think.
What I currently have:
SELECT AVG((SELECT AVG(f_total_ftg) as x FROM results WHERE f_datetime < '2020-07-01 01:30:00')) AS ft_x,
AVG((SELECT AVG(f_total_ftg) as x FROM results WHERE f_datetime between '2020-07-01 01:30:00' - INTERVAL 13 DAY AND '2020-07-01 01:30:00')) AS ft_y,
AVG((SELECT AVG(f_total_1mg) as x FROM results WHERE f_datetime < '2020-07-01 01:30:00')) AS 1m_x,
AVG((SELECT AVG(f_total_1mg) as x FROM results WHERE f_datetime between '2020-07-01 01:30:00' - INTERVAL 13 DAY AND '2020-07-01 01:30:00')) AS 1m_y,
AVG((SELECT AVG(f_total_htg) as x FROM results WHERE f_datetime < '2020-07-01 01:30:00')) AS ht_total,
AVG((SELECT AVG(f_total_htg) as x FROM results WHERE f_datetime between '2020-07-01 01:30:00' - INTERVAL 13 DAY AND '2020-07-01 01:30:00')) AS ht_last14d
FROM results
How can I simplify this?