对于一列中的相同 ID,如何为未来日期编写 CASE 语句?
对于下表,我想写一个CASE语句:对于同一个ID,如果多个日期是将来的并且Renew =“Yes”,则在Action栏中填写“Needs Review”。我假设应该使用 OVER(PARTITION BY() ,但不确定如何
原始表:
ID | Date | Renew |
---|---|---|
123 | 06-01-2023 | 是 |
123 | 06-01-2022 | 是 |
123 | 06-01-2021 | 是 |
预期结果(与CASE 声明):
ID | 日期 | 更新 | 操作 |
---|---|---|---|
123 | 06-01-2023 | 是 | 需要审核 |
123 | 06-01-2022 | 是 | 需要审查 |
123 | 06-01-2021 | 是 | 需要审查 |
For the following table, I want to write a CASE statement: For the same ID, if more than one Dates are in the future and Renew = "Yes," fill out the Action column with "Needs Review". I assume OVER(PARTITION BY() is supposed to be used, but not sure how
Original Table:
ID | Date | Renew |
---|---|---|
123 | 06-01-2023 | Yes |
123 | 06-01-2022 | Yes |
123 | 06-01-2021 | Yes |
Expected Outcome (with the CASE statement):
ID | Date | Renew | Action |
---|---|---|---|
123 | 06-01-2023 | Yes | Needs Review |
123 | 06-01-2022 | Yes | Needs Review |
123 | 06-01-2021 | Yes | Needs Review |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用条件 SUM() 来计算标记为“更新”的行数以及未来日期。如果计数 > 则返回“需要审核” 1.
您没有指定数据库管理系统,但尝试
SQL Server:
MySQL:
结果:
Try using a conditional SUM() to count the number of rows marked "Renew", with future dates. Then return "Needs Review" if the count is > 1.
You didn't specify your dbms, but try
SQL Server:
MySQL:
Results: