【问题标题】:Presto take monthly averagePresto 取月平均值
【发布时间】:2020-09-22 19:45:09
【问题描述】:
  • 我想为每个merchant_id 获取每月number_of_listings 的平均数量。

  • 下表显示了每个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

【问题讨论】:

    标签: sql average aggregate-functions presto date-arithmetic


    【解决方案1】:

    这是一个简单的聚合查询。您可以使用date function 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-09
      • 2021-04-04
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      相关资源
      最近更新 更多