【问题标题】:How do I impose LIKE operator on an array in Big Query?如何在 Big Query 中对数组施加 LIKE 运算符?
【发布时间】:2022-11-24 03:34:24
【问题描述】:

我有一个包含一组字符串的数组,我试图在另一列字符串类型中搜索这些字符串。基本上是一种 LIKE 运算符,但带有数组。

我有的:

I have two tables keyword_table and config_table.

表 1:“关键字表”

category(STRING)  keywords(ARRAY)
fruits            ["orange", "berry", "apple"]
vegetables        ["bean", "carrot", "onion"]

表 2:“配置表”

code(STRING)     item(STRING)
001              blueberry
002              raspberry
003              white onions
004              red onions
005              onion
006              small beans
007              big beans

预期输出:

code(STRING)     category(STRING)
001              fruits
002              fruits
003              vegetables
004              vegetables
005              vegetables
006              vegetables
007              vegetables

有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: sql arrays google-bigquery


    【解决方案1】:

    您可以应用以下 sql 查询:

    with keyword_table AS (
      select 
        "fruits" AS category,
         ["orange", "berry", "apple"] AS keywords
      UNION ALL
      select 
        "vegetables" AS category,
         ["bean", "carrot", "onion"] AS keywords
    ),
    
    config_table AS (
      select "001" AS code, "orange" AS item
      UNION ALL
      select "002" AS code, "raspberry" AS item
      UNION ALL
      select "003" AS code, "carrot" AS item
      UNION ALL
      select "004" AS code, "red onions" AS item
      UNION ALL
      select "005" AS code, "onion" AS item
      UNION ALL
      select "006" AS code, "small beans" AS item
      UNION ALL
      select "007" AS code, "big beans" AS item
    )
    
    select
      code,
      calculatedCategory as category
    from
    (
      select 
        code,
        item,
        category,
        keywords,
        CASE WHEN item = keyword then category 
              ELSE ''
              END AS calculatedCategory,
      from config_table 
      cross join keyword_table,
      UNNEST(keywords) AS keyword
    )
    where calculatedCategory <> '';
    

    结果是:

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 2017-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      相关资源
      最近更新 更多