Postgres根据日期的平均过滤器
我有一个Postgres表,每小时都会由Cron更新。我需要根据1天,5天,10天的平均卷。当我这样的平均水平时,
select avg(volume), id from price_table where NOW() > updated_at::timestamptz
AND NOW() - updated_at::timestamptz <= interval '5 day' group by id and order by avg(price).
这只需要所有条目的平均值,但我希望平均每天需要平均值,然后根据我们提供的天数添加所有天数。
我的桌子示例
id | volume| name | updated_at
1 | 56 | apple| 2022-05-12 03:06:53.981
1 | 75 | apple| 2022-05-12 03:06:53.981
2 | 57 | orang| 2022-05-12 03:06:53.981
2 | 56 | orang| 2022-05-12 03:06:53.981
1 | 56 | apple| 2022-05-13 01:00:02.793
1 | 56 | apple| 2022-05-13 01:00:02.793
2 | 76 | orang| 2022-05-13 01:00:02.793
1 | 56 | apple| 2022-05-13 04:06:53.981
1 | 56 | apple| 2022-05-14 01:00:02.530
1 | 56 | apple| 2022-05-14 01:00:02.530
2 | 16 | orang| 2022-05-14 01:00:02.530
1 | 56 | apple| 2022-05-14 03:06:53.981
1 | 56 | apple| 2022-05-15 03:06:53.981
1 | 56 | apple| 2022-05-15 03:06:53.981
1 | 56 | apple| 2022-05-15 03:06:53.981
1 | 56 | apple| 2022-05-15 03:06:53.981
1 | 56 | apple| 2022-05-16 03:06:53.981
2 | 16 | apple| 2022-05-16 03:06:53.981
1 | 56 | apple| 2022-05-16 03:06:53.981
1 | 56 | apple| 2022-05-16 03:06:53.981
1 | 56 | apple| 2022-05-16 03:06:53.981
因此,当我搜索7天的音量时,它需要每天的平均值,并将其总结为ID分组的7天,
任何人都可以帮助我解决这个问题
I am having a postgres table which is updated by a cron for every hour. I need average of that volume based on 1 day, 5 day, 10 day. when i average it like this
select avg(volume), id from price_table where NOW() > updated_at::timestamptz
AND NOW() - updated_at::timestamptz <= interval '5 day' group by id and order by avg(price).
This is taking only the average of all the entries but i want an average where it will take an average of each day and then add the average of all days based on the number of days we provide.
My table example
id | volume| name | updated_at
1 | 56 | apple| 2022-05-12 03:06:53.981
1 | 75 | apple| 2022-05-12 03:06:53.981
2 | 57 | orang| 2022-05-12 03:06:53.981
2 | 56 | orang| 2022-05-12 03:06:53.981
1 | 56 | apple| 2022-05-13 01:00:02.793
1 | 56 | apple| 2022-05-13 01:00:02.793
2 | 76 | orang| 2022-05-13 01:00:02.793
1 | 56 | apple| 2022-05-13 04:06:53.981
1 | 56 | apple| 2022-05-14 01:00:02.530
1 | 56 | apple| 2022-05-14 01:00:02.530
2 | 16 | orang| 2022-05-14 01:00:02.530
1 | 56 | apple| 2022-05-14 03:06:53.981
1 | 56 | apple| 2022-05-15 03:06:53.981
1 | 56 | apple| 2022-05-15 03:06:53.981
1 | 56 | apple| 2022-05-15 03:06:53.981
1 | 56 | apple| 2022-05-15 03:06:53.981
1 | 56 | apple| 2022-05-16 03:06:53.981
2 | 16 | apple| 2022-05-16 03:06:53.981
1 | 56 | apple| 2022-05-16 03:06:53.981
1 | 56 | apple| 2022-05-16 03:06:53.981
1 | 56 | apple| 2022-05-16 03:06:53.981
so when i search for the volume of 7 days, it needs to take the average of each day and sum it up for just the 7 days grouped by the id
Could anyone help me out on this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道您为什么要概括平均值。
就是说,将其分为两个部分。首先找到每日平均水平。然后总结最后七天的每日平均值:
db&lt; a>
I do not know why you are summing the averages.
That said, break it into two parts. First find the daily average. Then sum the last seven days' daily averages:
db<>fiddle here