【问题标题】:Is there a way to shorthand this CASE statement in Snowflake/SQL?有没有办法在 Snowflake/SQL 中简写这个 CASE 语句?
【发布时间】:2022-12-07 02:22:51
【问题描述】:

在这里有这个案例陈述,我想与“CASE WHEN”和“CASE ILIKE”一起使用,类似于以下内容:

SELECT
  CASE coalesce(lower(regexp_substr(random_sample_column, '\/key_word\/\\d+\/([\\w-]+)(\/|$)', 1, 1, 'e', 1)), random_sample_column)
     when 'green' then 'GO'
     when 'red' then 'STOP'
     when 'yellow' then 'SLOW'
     else
         case when coalesce(lower(regexp_substr(random_sample_column, '\/key_word\/\\d+\/([\\w-]+) ilike '%green' then 'GO?'
         case when coalesce(lower(regexp_substr(random_sample_column, '\/key_word\/\\d+\/([\\w-]+) ilike '%yellow' then 'SLOW?'
         case when coalesce(lower(regexp_substr(random_sample_column, '\/key_word\/\\d+\/([\\w-]+) ilike '%red' then 'STOP?'     
   END as sample_derived_column
FROM SAMPLE_TABLE_NAME;

有没有一种方法可以编写类似于代码块顶部“CASE WHEN”语句中使用的“速记”的(NESTED)类似案例语句?

提前致谢!

样本数据在这里:

random_sample_column
--------------
yellow30-1123$%schmidt
jingle43123heimer
red
isthelightgreen
beluw
beow
blue

【问题讨论】:

  • 你能发布一些样本数据吗?
  • 刚刚发布了一些@Rajat,谢谢!

标签: sql snowflake-cloud-data-platform


【解决方案1】:

一种可能的方法是使用列别名:

WITH cte AS (
SELECT *,
  coalesce(lower(regexp_substr(random_sample_column, '/key_word/\d+/([\w-]+)(/|$)', 1, 1, 'e', 1)), random_sample_column) AS helper_col1,
  coalesce(lower(regexp_substr(random_sample_column, '/key_word/\d+/([\w-]+') AS helper_col2,
  CASE helper_col1
     when 'green' then 'GO'
     when 'red' then 'STOP'
     when 'yellow' then 'SLOW'
     else
         case when helper_col2 ilike '%green' then 'GO?'
              when helper_col2 ilike '%yellow' then 'SLOW?'
              when helper_col2 ilike '%red' then 'STOP?' 
         end    
   END as sample_derived_column
FROM SAMPLE_TABLE_NAME
)
SELECT * EXCLUDE (helper_col1, helper_col2)
FROM cte;

嵌套不可读,所以:

WITH cte AS (
SELECT *,
  coalesce(lower(regexp_substr(random_sample_column, '/key_word/\d+/([\w-]+)(/|$)', 1, 1, 'e', 1)), random_sample_column) AS helper_col1,
  coalesce(lower(regexp_substr(random_sample_column, '/key_word/\d+/([\w-]+') AS helper_col2,
  CASE
     when helper_col1 =  'green'      then 'GO'
     when helper_col1 = 'red'         then 'STOP'
     when helper_col1 = 'yellow'      then 'SLOW'
     when helper_col2 ilike '%green'  then 'GO?'
     when helper_col2 ilike '%yellow' then 'SLOW?'
     when helper_col2 ilike '%red'    then 'STOP?'       
   END as sample_derived_column
FROM SAMPLE_TABLE_NAME
)
SELECT * EXCLUDE (helper_col1, helper_col2)
FROM cte;

编辑:

为了使用 LIKE 谓词和通配符执行简单的 case 表达式:

SELECT 
   CASE expresssion
     WHEN ILIKE '%green' THEN 'GO?'
     WHEN ILIKE '%yellow' THEN 'SLOW?'
     WHEN ILIKE '%red' THEN 'STOP?'
   END 
FROM SAMPLE_TABLE_NAME;

使用的 SQL 方言必须实现可选的 SQL:2003 Extended CASE expression(F262) 特性。

更多信息:SQL 'CASE WHEN x' vs. 'CASE x WHEN' with greater-than condition?

【讨论】:

  • 谢谢卢卡斯!如果我们不想使用列别名怎么办?可能不可能吧?
  • @JohnWick simple case 使用= 作为比较运算符。在 ELSE 部分,您的目标是使用通配符 ILIKE ,为此您需要使用 searched case
  • 谢谢卢卡斯。很抱歉问,但你能告诉我吗? :) (searched case)
  • @JohnWick 已经在答案中做到了。 CASE expression WHEN '%value% THEN ...没有语法,写成CASE WHEN expression LIKE '%value%' THEN ...
  • 抱歉,我不确定 Lukasz。感谢帮助!
猜你喜欢
  • 2020-07-31
  • 1970-01-01
  • 2021-07-03
  • 2023-03-26
  • 1970-01-01
  • 2021-06-08
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
相关资源
最近更新 更多