gpt4 book ai didi

sql - 如何在 WHERE 子句中编写条件?

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

请求分为 2 部分:

  1. 记录应从报告中脱落 _*如果记录已过期且if *_RecordStatus != '已资助'

  2. 记录应保留在报告中_如果 RecordStatus = 'Funded'*,没有*PURCHASEDATE,并且如果记录已过期(是从**当前日期过去)。

我尝试使用 CASE 执行此操作,但此请求似乎是 bool 逻辑,因此不切实际。我试图研究 IF...THEN...ELSE 但我不知道如何去做。我花了整整两天的时间试图让它发挥作用,但我一无所获。有没有人可以帮助我解决这个问题,然后解释一下?

下面是一个简单的失败示例(是的,我完全理解这是由于条件而不是表达式而失败):

Select distinct ...  
from ... --with joins
where ...
and case when RecordStatus != 'Funded' then getdate() <= EXPIRATIONDATE else ?? end

谢谢!!

最佳答案

这些要求对我来说似乎很可疑,但这是我对此的看法(不需要“条件逻辑”)以及为什么它看起来“可疑”,如果我正确地阅读它的话..

Records should stay on the report If RecordStatus = 'Funded', has no PURCHASEDATE, and if the Record has expired (is in the past from currentdate).

RecordStatus = 'Funded'
AND PurchaseDate IS NULL
AND ExpiredDate < CURRENT_TIMESTAMP

Records should fall off report If the Record has expired AND if RecordStatus != 'Funded'.

NOT (HasExpired AND RecordStatus <> 'Funded')

综合:

RecordStatus = 'Funded'
AND PurchaseDate IS NULL
AND ExpiredDate < CURRENT_TIMESTAMP
AND NOT (HasExpired AND RecordStatus <> 'Funded')

随着德摩根的转型:

RecordStatus = 'Funded'
AND PurchaseDate IS NULL
AND ExpiredDate < CURRENT_TIMESTAMP
AND (NOT HasExpired OR RecordStatus = 'Funded')

分发后消除包含条件。

RecordStatus = 'Funded'
AND PurchaseDate IS NULL
AND ExpiredDate < CURRENT_TIMESTAMP
AND NOT HasExpired

但是,假设HasExpiredExpiredDate < CURRENT_TIMESTAMP 相同

RecordStatus = 'Funded'
AND PurchaseDate IS NULL
AND ExpiredDate < CURRENT_TIMESTAMP
AND NOT ExpiredDate < CURRENT_TIMESTAMP

这当然不是真的,为什么我会受到怀疑。

关于sql - 如何在 WHERE 子句中编写条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21342052/

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