【发布时间】: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