gpt4 book ai didi

mysql - SQL按用户选择第二小的值

转载 作者:行者123 更新时间:2023-11-29 07:35:13 25 4
gpt4 key购买 nike

我想得到一个只包含第二次购买日期的 list

我有以下列表:

    Order_id     User_id       Purchase_date
1 1 01/01/2018
2 1 02/01/2018
3 1 02/01/2018
4 2 03/01/2018
5 2 03/01/2018
6 3 01/01/2018
7 3 02/01/2018

期望的结果:

    user_id       2nd_purchase_date
1 02/01/20181
2 03/01/2018
3 02/01/2018

谢谢

最佳答案

SQL DEMO

你可以像下面那样做:

select user_id, Purchase_date AS '2nd_purchase_date'
from yourtable
where order_id in (
select min(order_id)
from yourtable T1
where order_id > (
select min(order_id)
from yourtable
where user_id = T1.user_id
)
group by user_id
)

关于mysql - SQL按用户选择第二小的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49238824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com