使用多个时如何使用 CASE 条件 table

How to use CASE condition when using more than one table

我想用case语句来检查信用卡的有效期。我有两个表 -

  1. Table_A - account_no, account_id
  2. Table_B - account_no, account_status, date_of_card_expiry

以上字段的数据类型为- account_no - 数字,account_id - 数字,account_status - 数字,date_of_card_expiry - 字符

我必须编写查询来检查卡是否会在 30 天或 60 天后过期。下面是我的查询,但它在第 4 行给出了无效的关系运算符错误。

SELECT a.account_no, a.account_id, b.account_status,b.date_of_card_expiry
  FROM Table_A a
  JOIN Table_B b
    ON (a.account_no = b.account_no
   AND a.account_id IN ('1','2','7')
   AND (
         CASE WHEN (to_date(b.date_of_card_expiry, 'MMYY')) = to_date(to_char(sysdate+60, 'MMYY'),'MMYY') then 'card will expire after 2 months'
         CASE WHEN (to_date(b.date_of_card_expiry, 'MMYY')) = to_date(to_char(sysdate+30, 'MMYY'),'MMYY') then 'card will expire after 1 month'))

请帮我更正查询。

您需要使用条件作为列,如下所示。

我已经修改了条件逻辑,如下,如果今天和card_expiry_date之间的天数差异超过60天,那么它被标记为'card expires after 2 months'并且超过30天天,阅读'card expires after 1 month'。但我认为如果日期差异不符合两个标准中的任何一个,你还需要一个 else 部分来显示

SELECT a.account_no, a.account_id, b.account_status,b.date_of_card_expiry
       ,CASE WHEN  to_date(b.date_of_card_expiry, 'MMYY')-trunc(sysdate)>=60 then 
                  'card will expire after 2 months'
             WHEN to_date(b.date_of_card_expiry, 'MMYY')-trunc(sysdate)>=30  then 
                  'card will expire after 1 month'
        END as card_status
  FROM Table_A a
  JOIN Table_B b
    ON (a.account_no = b.account_no
   AND a.account_id IN ('1','2','7')