where 子句中的 case 语句,我的查询有什么问题?

发布于 2025-01-18 11:50:03 字数 549 浏览 0 评论 0原文

谁能告诉我我的代码怎么了?

SELECT 
    *
FROM    
    TABLE.NAME
WHERE 
    TICKET_GROUP IN 
        CASE WHEN ticket_created_at <= CAST('2022-04-01' AS DATE) 
                 THEN  (1500003613882, 360013692813, 114095263234) 
                 ELSE (1500001610322, 360005931333, 1260811380530) 
        END

我有这个错误

第8:20行:不匹配的输入'in in'。期待:'和',',“除”,“ fetch”,“ group”,“ have”,“ betsect”,“ limit”,“ offset”,“ offse”,“ or''',''','',''','''',&lt; eof&gt;

我尝试将其更改为“ =”,而不是IN,但它没有起作用。

Can anyone tell me what's wrong with my code?

SELECT 
    *
FROM    
    TABLE.NAME
WHERE 
    TICKET_GROUP IN 
        CASE WHEN ticket_created_at <= CAST('2022-04-01' AS DATE) 
                 THEN  (1500003613882, 360013692813, 114095263234) 
                 ELSE (1500001610322, 360005931333, 1260811380530) 
        END

I get this error

line 8:20: mismatched input 'IN'. Expecting: 'AND', 'EXCEPT', 'FETCH', 'GROUP', 'HAVING', 'INTERSECT', 'LIMIT', 'OFFSET', 'OR', 'ORDER', 'UNION', <EOF>

I tried changing it to '=' instead of IN but it didn't worked.

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

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

发布评论

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

评论(2

宣告ˉ结束 2025-01-25 11:50:03

案例表达式总是产生A value ,从来都不是其他表达式。因此,您必须构建其中条款更多类似:

WHERE 
   ( ticket_created_at <= CAST('2022-04-01' AS DATE) 
     AND TICKET_GROUP IN (1500003613882, 360013692813, 114095263234) 
   )
   OR (
     ticket_created_at > CAST('2022-04-01' AS DATE)
     AND TICKET_GROUP IN (1500001610322, 360005931333, 1260811380530) 
   )

此外,我们可以使用 nullif() 为了简化caseselect> Select子句中的表达式中的表达式:

SELECT 
    *,
    COALESCE(NULLIF(C_220_COUNTRY_CODE, 'N/A'), COUNTRY_SALES_POD) AS FINAL_COUNTRY_CODE

FROM    
    CUSTOMER_OPS_HISTORICAL.ZENDESK_HISTORICAL_TICKET_CONSOLIDATED_CUSTOMER_OPS_DASHBOARD
WHERE 
   ( ticket_created_at <= CAST('2022-04-01' AS DATE) 
     AND TICKET_GROUP IN (1500003613882, 360013692813, 114095263234) 
   )
   OR (
     ticket_created_at > CAST('2022-04-01' AS DATE)
     AND TICKET_GROUP IN (1500001610322, 360005931333, 1260811380530) 
   )

A CASE expression always produces a value, never another expression. Therefore you must structure the WHERE clause more like this:

WHERE 
   ( ticket_created_at <= CAST('2022-04-01' AS DATE) 
     AND TICKET_GROUP IN (1500003613882, 360013692813, 114095263234) 
   )
   OR (
     ticket_created_at > CAST('2022-04-01' AS DATE)
     AND TICKET_GROUP IN (1500001610322, 360005931333, 1260811380530) 
   )

Additionally, we can use NULLIF() to simplify the CASE expression in the SELECT clause like so:

SELECT 
    *,
    COALESCE(NULLIF(C_220_COUNTRY_CODE, 'N/A'), COUNTRY_SALES_POD) AS FINAL_COUNTRY_CODE

FROM    
    CUSTOMER_OPS_HISTORICAL.ZENDESK_HISTORICAL_TICKET_CONSOLIDATED_CUSTOMER_OPS_DASHBOARD
WHERE 
   ( ticket_created_at <= CAST('2022-04-01' AS DATE) 
     AND TICKET_GROUP IN (1500003613882, 360013692813, 114095263234) 
   )
   OR (
     ticket_created_at > CAST('2022-04-01' AS DATE)
     AND TICKET_GROUP IN (1500001610322, 360005931333, 1260811380530) 
   )
御守 2025-01-25 11:50:03

如果要在值中“动态”定义,则可以在 case>案例> case> case> case>语句的结果表达式中移动ticket_group:

WHERE 
    CASE 
       WHEN ticket_created_at <= CAST('2022-04-01' AS DATE) 
          THEN TICKET_GROUP IN (1500003613882, 360013692813, 114095263234) 
       ELSE TICKET_GROUP IN (1500001610322, 360005931333, 1260811380530) 
    END

或者只需使用布尔值具有的逻辑:

WHERE 
    (ticket_created_at <= CAST('2022-04-01' AS DATE) AND TICKET_GROUP IN (1500003613882, 360013692813, 114095263234))
    OR (TICKET_GROUP IN (1500001610322, 360005931333, 1260811380530))

If you want to "dynamically" define the IN values then you can move TICKET_GROUP IN inside the case statement's result expressions:

WHERE 
    CASE 
       WHEN ticket_created_at <= CAST('2022-04-01' AS DATE) 
          THEN TICKET_GROUP IN (1500003613882, 360013692813, 114095263234) 
       ELSE TICKET_GROUP IN (1500001610322, 360005931333, 1260811380530) 
    END

Or just use the boolean logic with OR and AND:

WHERE 
    (ticket_created_at <= CAST('2022-04-01' AS DATE) AND TICKET_GROUP IN (1500003613882, 360013692813, 114095263234))
    OR (TICKET_GROUP IN (1500001610322, 360005931333, 1260811380530))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文