BigQuery - 标量子查询产生了不止一个元素 -

BigQuery - Scalar subquery produced more than one element -

我有这个查询可以帮助我在字符串中找到单独的关键词(对于 utm_campaign 和 utm_content 非常有用):

    SELECT
    utm_campaign,
    splits[SAFE_OFFSET(0)] AS country,
    splits[SAFE_OFFSET(1)] AS product,
    splits[SAFE_OFFSET(2)] AS budget,
    splits[SAFE_OFFSET(3)] AS source,
    splits[SAFE_OFFSET(4)] AS campaign,
    splits[SAFE_OFFSET(5)] AS audience
    
    FROM (
      SELECT
        utm_campaign,
        SPLIT(REGEXP_REPLACE(
                utm_campaign,
                r'([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_(.+)',
                r'||||||'),
              '|') AS splits
      FROM funnel_campaign)

例如:如果我有这样的umt_campaign:

us_latam_mkt_google_black-friday_audiencie-custom_NNN-NNN_nnn_trafic_responsiv

上面的查询将帮助我用 _ 分隔每个单词。所以我会得到这样的结果:

utm_campaign country product budget source campaign audience
us_latam_mkt_google_black-friday_audiencie-custom_NNN-NNN_nnn_trafic_responsiv us latam mkt google black-friday audience-custom

    WITH COST AS  (
    
    SELECT 
    
    POS AS POS,
    DATE AS DATE,
    EXTRACT(WEEK FROM DATE) AS WEEK,
    SOURCE AS SOURCE,
    MEDIUM AS MEDIUM,
    CAMPAIGN AS CAMPAIGN,
    AD_CONTENT,
    FORMAT AS FORMAT,
    "" AS BU_OD,
    SUM(CLICKS)/1000 AS CLICKS,
    SUM(IMPRESSIONS)/1000 AS IMPRESSIONS,
    SUM(COST)/1000 AS COST,
    sum(0) as SESSIONS,
    SUM(0) AS TRANSACTIONS,
    SUM(0) AS search_flight_pv,
    SUM(0) AS search_flight_upv,
    SUM(0) AS PAX,
    SUM(0) AS REVENUE,
    
    FROM MSR_funnel_campaign_table 
    WHERE DATE >= DATE '2019-01-01' 
    AND MEDIUM NOT LIKE 'DISPLAY_CORP' 
    GROUP BY 1,2,3,4,5,6,7,8,9
    ),
    
    REVENUE AS(
    SELECT
    POS AS POS,
    date AS DATE,
    EXTRACT(WEEK FROM DATE) AS WEEK,
    SOURCE_CAT AS SOURCE,
    medium_group_2 AS MEDIUM,
    CAMPAIGN AS CAMPAIGN,
    AD_CONTENT,
    CASE
      WHEN SOURCE_CAT = 'FACEBOOK' THEN 
    (
    
    SELECT
    splits[SAFE_OFFSET(5)] AS FORMAT,
   
    FROM  (
      SELECT
        ad_content,
        SPLIT(REGEXP_REPLACE(
                ad_content,
                r'([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_(.+)',
                r'||||||'),
              '|') AS splits 
    FROM ga_digital_marketing)) END AS FORMAT,
   
    
    BU_OD AS BU_OD,
    SUM(0) AS CLICKS,
    SUM(0) AS IMPRESSIONS,
    SUM(0) AS COST,
    sum(sessions)/1000 as SESSIONS,
    SUM(TRANSACTIONS)/1000 AS TRANSACTIONS,
    SUM(search_flight_pv)/1000 AS search_flight_pv,
    SUM(search_flight_upv)/1000 AS search_flight_upv,
    SUM(PAX)/1000 AS PAX,
    SUM(REVENUE)/1000 AS REVENUE,  
    
    FROM ga_digital_marketing 
    WHERE PAX_TYPE = 'PAID'
    AND DATE >= DATE '2019-01-01'
    AND MEDIUM NOT LIKE 'DISPLAY_CORP'
    GROUP BY 1,2,3,4,5,6,7,8,9
    ),
    
    COST_REVENUE AS (
    SELECT *
    FROM COST
    
    UNION ALL
    
    SELECT *
    FROM REVENUE
    
    )
    
    SELECT
    DATE,
    WEEK,
    POS,
    SOURCE,
    MEDIUM,
    CAMPAIGN,
    AD_CONTENT,
    FORMAT,
    BU,
    CLICKS,
    IMPRESSIONS,
    SESSIONS,
    TRANSACTIONS,
    search_flight_pv,
    search_flight_upv,
    COST,
    PAX,
    REVENUE,
    
    FROM COST_REVENUE
    
    WHERE
     1=1 
    AND DATE >= '2019-01-01'

我做错了什么?

我也希望看到来自 COST 的格式维度与来自 REVENUE 的格式维度之间存在匹配(它不存在,但在活动列中)。

您实际上不需要内部 select 语句,因为您的广告系列数据应该位于 table.

的同一行中

改变这个:

    CASE
      WHEN SOURCE_CAT = 'FACEBOOK' THEN 
    (
    
    SELECT
    splits[SAFE_OFFSET(5)] AS FORMAT,
   
    FROM  (
      SELECT
        ad_content,
        SPLIT(REGEXP_REPLACE(
                ad_content,
                r'([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_(.+)',
                r'||||||'),
              '|') AS splits 
    FROM ga_digital_marketing)) END AS FORMAT,

像这样:

    -- also replacing case with if for only 1 case
    IF(SOURCE_CAT = 'FACEBOOK', 
            SPLIT(REGEXP_REPLACE(
                ad_content,
                r'([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_(.+)',
                r'||||||'),
              '|')[SAFE_OFFSET(5)], NULL) AS FORMAT,