获取用户第 2 个月交易的所有交易明细
Get all transaction details of a user f their 2nd month of transaction
正在尝试获取所有客户的第 2 个交易月份详细信息
Date User_id amount
2021-11-01 1 100
2021-11-21 1 200
2021-12-20 2 110
2022-01-20 2 200
2022-02-04 1 50
2022-02-21 1 100
2022-03-22 2 200
对于每个客户获取他们第二次交易当月的所有记录(特定用户在一个月和一天内可以有多次交易)
预期输出
Date User_id amount
2022-02-04 1 50
2022-02-21 1 100
2022-01-20 2 200
如果 dense_rank
是一个选项,您可以:
with cte1 as (
select *, extract(year_month from date) as yyyymm
from t
), cte2 as (
select *, dense_rank() over (partition by user_id order by yyyymm) as dr
from cte1
)
select *
from cte2
where dr = 2
请注意,可以使用一个 cte 编写上述内容。
您可以使用 dense_rank
:
select Date, User_id, amount from
(select *, dense_rank() over(partition by User_id order by year(Date), month(date)) r
from table_name) t
where r = 2;
正在尝试获取所有客户的第 2 个交易月份详细信息
Date User_id amount
2021-11-01 1 100
2021-11-21 1 200
2021-12-20 2 110
2022-01-20 2 200
2022-02-04 1 50
2022-02-21 1 100
2022-03-22 2 200
对于每个客户获取他们第二次交易当月的所有记录(特定用户在一个月和一天内可以有多次交易)
预期输出
Date User_id amount
2022-02-04 1 50
2022-02-21 1 100
2022-01-20 2 200
如果 dense_rank
是一个选项,您可以:
with cte1 as (
select *, extract(year_month from date) as yyyymm
from t
), cte2 as (
select *, dense_rank() over (partition by user_id order by yyyymm) as dr
from cte1
)
select *
from cte2
where dr = 2
请注意,可以使用一个 cte 编写上述内容。
您可以使用 dense_rank
:
select Date, User_id, amount from
(select *, dense_rank() over(partition by User_id order by year(Date), month(date)) r
from table_name) t
where r = 2;