Postgres根据日期的平均过滤器

发布于 2025-02-07 20:40:56 字数 1366 浏览 1 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捎一片雪花 2025-02-14 20:40:56

我不知道您为什么要概括平均值。

就是说,将其分为两个部分。首先找到每日平均水平。然后总结最后七天的每日平均值:

with daily_avgs as (
  select id, updated_at::date as ddate, avg(volume) as day_avg
    from price_table
   group by id, ddate
)
select id, ddate, 
       sum(day_avg) over (partition by id
                              order by ddate
                      range between '7 days' preceding
                                and current row) as sum_day_avg
  from daily_avgs;

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:

with daily_avgs as (
  select id, updated_at::date as ddate, avg(volume) as day_avg
    from price_table
   group by id, ddate
)
select id, ddate, 
       sum(day_avg) over (partition by id
                              order by ddate
                      range between '7 days' preceding
                                and current row) as sum_day_avg
  from daily_avgs;

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文