【问题标题】:Want to add third columns when using aggregate funtion, that does not come under group by想要在使用聚合函数时添加第三列,不属于 group by
【发布时间】:2021-10-25 02:13:31
【问题描述】:

这是我查询后的输出 第一步:

select customer_id, plan_id, start_date, row_number() over (partition by customer_idorder by plan_id) as row_order
    from f.subscriptions
    where start_date < '2021-01-01'

customer_id plan_id start_date  row_order
1   0   2020-08-01T00:00:00.000Z    1
1   1   2020-08-08T00:00:00.000Z    2
2   0   2020-09-20T00:00:00.000Z    1
2   3   2020-09-27T00:00:00.000Z    2
3   0   2020-01-13T00:00:00.000Z    1
3   1   2020-01-20T00:00:00.000Z    2
4   0   2020-01-17T00:00:00.000Z    1
4   1   2020-01-24T00:00:00.000Z    2
4   4   2020-04-21T00:00:00.000Z    3
5   0   2020-08-03T00:00:00.000Z    1
5   1   2020-08-10T00:00:00.000Z    2
6   0   2020-12-23T00:00:00.000Z    1
6   1   2020-12-30T00:00:00.000Z    2
7   0   2020-02-05T00:00:00.000Z    1
7   1   2020-02-12T00:00:00.000Z    2
7   2   2020-05-22T00:00:00.000Z    3
8   0   2020-06-11T00:00:00.000Z    1
8   1   2020-06-18T00:00:00.000Z    2
8   2   2020-08-03T00:00:00.000Z    3

第二步:

select customer_id, max(row_order)
from (select customer_id, plan_id, start_date
           , row_number() over (partition by customer_id order by plan_id) as row_order
      from f.subscriptions
      where start_date < '2021-01-01') t1
group by customer_id

输出:

customer_id max
1   2
2   2
3   2
4   3
5   2
6   2
7   3
8   3

现在我还想在第二个输出中添加 column(plan_id)。该怎么做?

【问题讨论】:

  • 很抱歉没有很好地复制。
  • 你想要哪个 oan_id 作为数组最大值或其他什么

标签: sql postgresql group-by aggregate-functions


【解决方案1】:

如果我理解正确,您希望每个客户的完整行包含最新的start_date。在 Postgres 中,最好的方法是使用distinct on

select distinct on (customer_id) s.*
from f.subscriptions s
where start_date < '2021-01-01'
order by customer_id start_date desc;

【讨论】:

    【解决方案2】:

    这取决于您的需要和想要什么,因为每个 id 有多个计划

    您还可以查看更多aggregation funczions

    CREATE TABLE subscriptions
        ("customer_id" int, "plan_id" int, "start_date" TIMESTAMP)
    ;
        
    INSERT INTO subscriptions
        ("customer_id", "plan_id", "start_date")
    VALUES
        (1, 0, '2020-08-01T00:00:00.000Z'),
        (1, 1, '2020-08-08T00:00:00.000Z'),
        (2, 0, '2020-09-20T00:00:00.000Z'),
        (2, 3, '2020-09-27T00:00:00.000Z'),
        (3, 0, '2020-01-13T00:00:00.000Z'),
        (3, 1, '2020-01-20T00:00:00.000Z'),
        (4, 0, '2020-01-17T00:00:00.000Z'),
        (4, 1, '2020-01-24T00:00:00.000Z'),
        (4, 4, '2020-04-21T00:00:00.000Z'),
        (5, 0, '2020-08-03T00:00:00.000Z'),
        (5, 1, '2020-08-10T00:00:00.000Z'),
        (6, 0, '2020-12-23T00:00:00.000Z'),
        (6, 1, '2020-12-30T00:00:00.000Z'),
        (7, 0, '2020-02-05T00:00:00.000Z'),
        (7, 1, '2020-02-12T00:00:00.000Z'),
        (7, 2, '2020-05-22T00:00:00.000Z'),
        (8, 0, '2020-06-11T00:00:00.000Z'),
        (8, 1, '2020-06-18T00:00:00.000Z'),
        (8, 2, '2020-08-03T00:00:00.000Z')
    ;
    
    select customer_id, max(plan_id), MAX(start_date), max(row_order)
    from (select customer_id, plan_id, start_date
               , row_number() over (partition by customer_id order by plan_id) as row_order
          from subscriptions
          where start_date < '2021-01-01') t1
    group by customer_id
    
    客户 ID |最大 |最大 |最大限度 ----------: | --: | :----------------- | --: 1 | 1 | 2020-08-08 00:00:00 | 2 2 | 3 | 2020-09-27 00:00:00 | 2 3 | 1 | 2020-01-20 00:00:00 | 2 4 | 4 | 2020-04-21 00:00:00 | 3 5 | 1 | 2020-08-10 00:00:00 | 2 6 | 1 | 2020-12-30 00:00:00 | 2 7 | 2 | 2020-05-22 00:00:00 | 3 8 | 2 | 2020-08-03 00:00:00 | 3
    select customer_id, ARRAY_AGG (plan_id || '-' || start_date), max(row_order)
    from (select customer_id, plan_id, start_date
               , row_number() over (partition by customer_id order by plan_id) as row_order
          from subscriptions
          where start_date < '2021-01-01') t1
    group by customer_id
    
    客户 ID | array_agg |最大限度 ----------: | :------------------------------------------------ ------------------------------------ | --: 1 | {"0-2020-08-01 00:00:00","1-2020-08-08 00:00:00"} | 2 2 | {"0-2020-09-20 00:00:00","3-2020-09-27 00:00:00"} | 2 3 | {"0-2020-01-13 00:00:00","1-2020-01-20 00:00:00"} | 2 4 | {"0-2020-01-17 00:00:00","1-2020-01-24 00:00:00","4-2020-04-21 00:00:00"} | 3 5 | {"0-2020-08-03 00:00:00","1-2020-08-10 00:00:00"} | 2 6 | {"0-2020-12-23 00:00:00","1-2020-12-30 00:00:00"} | 2 7 | {"0-2020-02-05 00:00:00","1-2020-02-12 00:00:00","2-2020-05-22 00:00:00"} | 3 8 | {"0-2020-06-11 00:00:00","1-2020-06-18 00:00:00","2-2020-08-03 00:00:00"} | 3

    db小提琴here

    【讨论】:

      猜你喜欢
      • 2016-10-28
      • 2018-11-07
      • 2014-08-24
      • 2017-09-16
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-27
      相关资源
      最近更新 更多