如何在BigQuery(标准SQL)中实现“ LIKE ANY”?

时间:2019-02-12 08:23:04

标签: google-bigquery

我想使用“任意”功能。但是,BigQuery无法识别“ ANY”运算符。有什么功能可以代替“ LIKE ANY”吗?

Bigquery-标准SQL

    SELECT
      date,
      CONCAT(fullvisitorid, CAST(visitid AS string)) ord_browsers,
      hits.appInfo.landingScreenName,
      channelgrouping,
      trafficSource.source,
      trafficSource.medium,
      CASE
        WHEN channelgrouping = 'seo_brand' THEN 'Branded'
        WHEN hits.appInfo.landingScreenName LIKE '%/article%'
      OR hits.appInfo.landingScreenName LIKE '%/p/%' THEN 'ADS'
        WHEN (hits.appInfo.landingScreenName LIKE ANY ('%/damesmode/%',  '%/herenkleding/%',  '%/kinderkleding/%',  '%/sportkleding/%')) THEN 'Plist'
        ELSE 'rest'
      END Landing_page
    FROM
      `meta-geography-145406.81187684.ga_sessions_*`,
      UNNEST(hits) AS hits
    WHERE
      (_table_suffix BETWEEN '20190201'
        AND '20190207')

语法错误:[12:47]处出现意外的关键字ANY

3 个答案:

答案 0 :(得分:2)

具有相同问题的简单查询:

WITH table AS (
  SELECT 'XXXX/herenkleding/XXX' x
)

SELECT x
FROM TABLE
WHERE x LIKE ANY ( 
  '%/damesmode/%'  '%/herenkleding/%',  '%/kinderkleding/%',  '%/sportkleding/%'
)

Syntax error: Unexpected keyword ANY at [2:6]

替代方法1:替换为一系列“喜欢或”

SELECT x
FROM TABLE
WHERE (x LIKE '%/damesmode/%' 
OR x LIKE '%/herenkleding/%'
OR x LIKE '%/kinderkleding/%'
OR x LIKE'%/sportkleding/%')

XXXX/herenkleding/XXX

替代2:组合成一个正则表达式

WITH table AS (
  SELECT 'XXXX/herenkleding/XXX' x
)

SELECT x
FROM TABLE
WHERE REGEXP_CONTAINS(x
  , '/(damesmode|herenkleding|kinderkleding|sportkleding)/')

XXXX/herenkleding/XXX

答案 1 :(得分:0)

这是另一种方法。在实现您想要的内容的同时,这将保持原始查询的完整性:

 SELECT
      date,
      CONCAT(fullvisitorid, CAST(visitid AS string)) ord_browsers,
      hits.appInfo.landingScreenName,
      channelgrouping,
      trafficSource.source,
      trafficSource.medium,
      CASE
        WHEN channelgrouping = 'seo_brand' THEN 'Branded'
        WHEN hits.appInfo.landingScreenName LIKE '%/article%'
      OR hits.appInfo.landingScreenName LIKE '%/p/%' THEN 'ADS'
        WHEN (hits.appInfo.landingScreenName LIKE c) THEN 'Plist'
        ELSE 'rest'
      END Landing_page
    FROM
      `meta-geography-145406.81187684.ga_sessions_*`,
      UNNEST(hits) AS hits, UNNEST([
       '%/damesmode/%',
       '%/herenkleding/%',
       '%/kinderkleding/%',
       '%/sportkleding/%']) AS c
    WHERE
      (_table_suffix BETWEEN '20190201'
        AND '20190207')

请注意,该数组已取消嵌套,并且在CASE语句中正在比较每个项目。

答案 2 :(得分:0)

以下是用于BigQuery标准SQL

   
#standardSQL
SELECT
  DATE,
  CONCAT(fullvisitorid, CAST(visitid AS STRING)) ord_browsers,
  hits.appInfo.landingScreenName,
  channelgrouping,
  trafficSource.source,
  trafficSource.medium,
  CASE
    WHEN channelgrouping = 'seo_brand' THEN 'Branded'
    WHEN REGEXP_CONTAINS(hits.appInfo.landingScreenName , '/(article|p)/') THEN 'ADS'
    WHEN REGEXP_CONTAINS(hits.appInfo.landingScreenName , '/(damesmode|herenkleding|kinderkleding|sportkleding)/') THEN 'Plist'
    ELSE 'rest'
  END Landing_page
FROM `meta-geography-145406.81187684.ga_sessions_*`,
UNNEST(hits) AS hits
WHERE _TABLE_SUFFIX BETWEEN '20190201' AND '20190207'