我的数据包含一列和JSON格式不的ID数组。
"100,101,102"
我希望能够查询类似
的行WHERE "101" IN idarray
但似乎不支持ARRAY类型作为列类型。
BigQuery中是否有一种方法可以查询包含元素的数组或分隔字符串?
例如在PostgreSQL中,我可以使用数组contains元素语法 @>
答案 0 :(得分:2)
SELECT *
FROM (
SELECT idarray FROM
(SELECT "100,101,102" AS idarray),
(SELECT "200,201,202,1010" AS idarray),
) as YourTable
WHERE "," + idarray + "," CONTAINS "," + "101" + ","
与上述相同,但“采用”BigQuery Standard SQL
WITH YourTable AS (
SELECT "100,101,102" AS idarray UNION ALL
SELECT "200,201,202,1010" AS idarray
)
SELECT *
FROM YourTable
WHERE CONCAT(",", idarray, ",") LIKE CONCAT("%,", "101", ",%")
或
WITH YourTable AS (
SELECT "100,101,102" AS idarray UNION ALL
SELECT "200,201,202,1010" AS idarray
)
SELECT *
FROM YourTable
WHERE REGEXP_CONTAINS(idarray, r"\b101\b")
或(“纯”标准SQL样式)
WITH YourTable AS (
SELECT "100,101,102" AS idarray UNION ALL
SELECT "200,201,202,1010" AS idarray
)
SELECT idarray
FROM YourTable, UNNEST(SPLIT(idarray)) AS id1
WHERE id1 = "101"