对于一列中的相同 ID,如何为未来日期编写 CASE 语句?

发布于 2025-01-16 20:58:26 字数 785 浏览 4 评论 0原文

对于下表,我想写一个CASE语句:对于同一个ID,如果多个日期是将来的并且Renew =“Yes”,则在Action栏中填写“Needs Review”。我假设应该使用 OVER(PARTITION BY() ,但不确定如何

原始表:

IDDateRenew
12306-01-2023
12306-01-2022
12306-01-2021

预期结果(与CASE 声明):

ID日期更新操作
12306-01-2023需要审核
12306-01-2022需要审查
12306-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:

IDDateRenew
12306-01-2023Yes
12306-01-2022Yes
12306-01-2021Yes

Expected Outcome (with the CASE statement):

IDDateRenewAction
12306-01-2023YesNeeds Review
12306-01-2022YesNeeds Review
12306-01-2021YesNeeds Review

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别理我 2025-01-23 20:58:26

尝试使用条件 SUM() 来计算标记为“更新”的行数以及未来日期。如果计数 > 则返回“需要审核” 1.

您没有指定数据库管理系统,但尝试

SQL Server:

SELECT t.*
       , IF(t.NumToReview > 1, 'Needs Review', NULL) AS [Action]
FROM (
       SELECT 
          Id
         , [Date]
         , [Renew]
         , SUM( CASE WHEN Date > cast(getDate() as date) 
                       AND Renew = 'Yes' THEN 1
                ELSE 0
           END
         ) OVER(PARTITION BY Id) AS NumToReview
      FROM  YourTable     
  ) t 

MySQL:

SELECT t.*
       , IF(t.NumToReview > 1, 'Needs Review', NULL) AS Action
FROM (
       SELECT 
          Id
         , `Date`
         , Renew
         , SUM( CASE WHEN `Date` > CAST(now() AS DATE) 
                       AND Renew = 'Yes' THEN 1
                ELSE 0
           END
         ) OVER(PARTITION BY Id) AS NumToReview
      FROM  YourTable  
  ) t 

结果:

IdDateRenewNumToReviewAction
12306-01- 20232需要审查
12306-01-20222需要审查
12306-01-20212需要审查

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:

SELECT t.*
       , IF(t.NumToReview > 1, 'Needs Review', NULL) AS [Action]
FROM (
       SELECT 
          Id
         , [Date]
         , [Renew]
         , SUM( CASE WHEN Date > cast(getDate() as date) 
                       AND Renew = 'Yes' THEN 1
                ELSE 0
           END
         ) OVER(PARTITION BY Id) AS NumToReview
      FROM  YourTable     
  ) t 

MySQL:

SELECT t.*
       , IF(t.NumToReview > 1, 'Needs Review', NULL) AS Action
FROM (
       SELECT 
          Id
         , `Date`
         , Renew
         , SUM( CASE WHEN `Date` > CAST(now() AS DATE) 
                       AND Renew = 'Yes' THEN 1
                ELSE 0
           END
         ) OVER(PARTITION BY Id) AS NumToReview
      FROM  YourTable  
  ) t 

Results:

IdDateRenewNumToReviewAction
12306-01-2023Yes2Needs Review
12306-01-2022Yes2Needs Review
12306-01-2021Yes2Needs Review
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文