gpt4 book ai didi

sql - 如何选择日期大于另一行中的日期的行?

转载 作者:行者123 更新时间:2023-12-02 09:18:14 25 4
gpt4 key购买 nike

我有一张 table ,上面有顾客卡。一个客户可以拥有多张卡。每张客户卡都有一行。如果客户有几张卡,则有几行。有两种不同的卡类型 A 和 B。对于每张卡,都会存储 last_used 日期。

我想检查是否有客户同时拥有 A 类型和 B 类型的客户卡,其中 B 卡的上次使用日期比 A 卡的上次使用日期更早。

这是我的表格 CUSTOMER_CARDS。

card_id | customer_id | card_type | last_used
----------------------------------------------
1 | c1 | A | 2017-07-07
2 | c1 | B | 2016-04-01
3 | c2 | A | 2017-06-04
4 | c2 | B | 2017-07-03
5 | c3 | A | 2016-02-23
6 | c3 | B | 2017-04-17

我需要一个 select 语句来选择客户 c2customer_id,该客户拥有 B 类型的卡,该卡比 A 类型的卡更年轻。我该如何做实现这个目标吗?

最佳答案

您需要Group ByHaving子句

select customer_id
from yourtable
Group by customer_id
Having Min(case when card_type = 'B' then last_used end) > Max(case when card_type = 'A' then last_used end)

关于sql - 如何选择日期大于另一行中的日期的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44971091/

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