无论如何要缩短我的bigquery脚本(for循环?)

bgibtngc  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(310)

好吧,我觉得我错过了一些基本的东西。t、 t型

SELECT
    CATEGORY,
    PRODUCT,
    (CASE
         WHEN PRODUCT_DETAIL = 'STRAWBERRY' AND some_condition = x THEN 'REQUIRE REVIEW'
         WHEN PRODUCT_DETAIL = 'COD' AND some_condition = x THEN 'REQUIRE REVIEW'
         WHEN PRODUCT_DETAIL = 'MILK' AND some_condition = x THEN 'REQUIRE REVIEW'
         WHEN PRODUCT_DETAIL = 'BREAD' AND some_condition = x THEN 'REQUIRE REVIEW'
         ...
         WHEN PRODUCT_DETAIL = 'ICE CREAM' AND some_condition = x THEN 'REQUIRE REVIEW'
     ELSE 'GOOD'
     END) AS PRODUCT_CONDITION 

FROM
    MAIN_DATABASE

我想我的问题是,既然{and some\u condition=x then'require review}都是一样的,有没有办法把代码缩短成某种形式的

WHEN PRODUCT_DETAIL = ('STRAWBERRY', 'COD', 'MILK', ... , 'ICE CREAM') AND some_condition = x
    THEN 'REQUIRE REVIEW'
    ELSE 'GOOD'

或者它的一些变体?
谢谢,cn

5gfr0r5j

5gfr0r5j1#

你可以用 IN :

(CASE WHEN PRODUCT_DETAIL IN ('STRAWBERRY' , 'COD', . . . ) AND
           some_condition = x
      THEN 'REQUIRE REVIEW'
      ELSE 'GOOD'
 END) AS PRODUCT_CONDITION

相关问题