0

我有这个查询可以帮助我在字符串中找到单独的关键词(对 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'\1|\2|\3|\4|\5|\6|\7'),
              '|') AS splits
      FROM funnel_campaign)

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

us_latam_mkt_google_black-friday_audiencie-custom_NNN-NNN_nnn_trafic_responsiv

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

utm_campaign 国家 产品 预算 资源 活动 观众
us_latam_mkt_google_black-friday_audiencie-custom_NNN-NNN_nnn_trafic_responsiv 我们 拉丁美洲 MKT 谷歌 黑色星期五 观众自定义
  • 我从上面的查询中想要的是在这种情况下给我audience列。我尝试将上面的查询添加为 REVENUE 中此查询的子查询,因为在此表中我没有audience列,但我有utm_campaign列。在 utm_campaign 字符串中,第六个片段是观众(使用此查询我有错误“标量子查询产生了多个元素”):

    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'\1|\2|\3|\4|\5|\6|\7'),
              '|') 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 的格式维度之间存在匹配(它不存在,但它在活动列中)。

4

1 回答 1

1

您实际上并不需要内部select语句,因为您的活动数据应该在表格的同一行中。

改变这个:

    CASE
      WHEN SOURCE_CAT = 'FACEBOOK' THEN 
    (
    
    SELECT
    splits[SAFE_OFFSET(5)] AS FORMAT,
   
    FROM  (
      SELECT
        ad_content,
        SPLIT(REGEXP_REPLACE(
                ad_content,
                r'([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_([^_]+)_(.+)',
                r'\1|\2|\3|\4|\5|\6|\7'),
              '|') 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'\1|\2|\3|\4|\5|\6|\7'),
              '|')[SAFE_OFFSET(5)], NULL) AS FORMAT,
   
于 2021-12-07T23:38:44.693 回答