预取月平均值

xmq68pz9  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(381)

我想取平均每月 number_of_listings 对于每个 merchant_id .
下表显示了每个 merchant_id 每日 number_of_listings 主表

date          merchant_id   number_of_listings
2019-02-01    12            325
2019-02-02    12            332
2019-02-03    12            235
2019-02-04    12            393
2019-02-05    12            484
2019-02-06    12            383
2019-02-07    12            434

输出表

month          merchant_id   average_number_of_listings
2019-02        12            400
643ylb08

643ylb081#

这是一个简单的聚合查询。你可以使用日期函数 date_trunc() ,返回每月的第一天:

select
    date_trunc('month', date) date_month,
    merchant_id
    avg(number_of_listings) average_number_of_listings
from mytable
group by date_trunc('month', date), merchant_id

相关问题