2021-08-31

Optimize postgres query avg by month

we have a report based on avg prices

basically, we filter those prices based on code and date and use UNION for each month

Table example:

date code price
2020-01-01 'FS-H21' 150
2020-01-01 'FS-G21' 155
2020-01-02 'FS-H21' 151
2020-01-03 'FS-G21' 148

and the query example:

SELECT period, code, avg(closure)
FROM period_prices
WHERE code = 'FS-F21' AND period BETWEEN '2020-01-01' AND '2020-01-31'
GROUP BY period, code

UNION

SELECT period, code, avg(closure)
FROM period_prices
WHERE code = 'FS-F21' AND period BETWEEN '2020-02-01' AND '2020-02-29'
GROUP BY period, code

...

Reproduced table and query at sample query...

I would like some advice on how can i improve the performance of that query, or if there is a better way to achieve this!

thanks



from Recent Questions - Stack Overflow https://ift.tt/3mMxwvU
https://ift.tt/eA8V8J

No comments:

Post a Comment