代码之家  ›  专栏  ›  技术社区  ›  HKE

在bigquery中,不使用自定义维度会复制/增加交易收入

  •  1
  • HKE  · 技术社区  · 7 年前

    不必要的hits.customdimension和hits.product.customdimension正在增加交易收入

    SELECT
         sum(totals.totalTransactionRevenue)/1000000 as revenue,
         (SELECT MAX(IF(index=10,value,NULL)) FROM UNNEST(product.customDimensions)) AS product_CD10,
         (SELECT MAX(IF(index=1,value,NULL)) FROM UNNEST(hits.customDimensions)) AS CD1
        FROM
          `XXXXXXXXXXXXXXX.ga_sessions_*`, 
          UNNEST(hits) AS hits, 
          UNNEST(hits.product) as product
         WHERE
          _TABLE_SUFFIX BETWEEN "20180608"
          AND "20180608"
          group by product_CD10,CD1
    

    有没有一种方法可以让我得到一个平面表,这样,如果我应用总收入,它应该给出正确的结果。

    1 回复  |  直到 7 年前
        1
  •  2
  •   Felipe Hoffa    7 年前

    将unnest()移动到顶部的子查询-这样行就不会重复:

    SELECT row
     ,  (SELECT MAX(letter) FROM UNNEST(row), UNNEST(qq)) max_letter
     ,  (SELECT MAX(n) FROM UNNEST(row), UNNEST(qq), UNNEST(qb) n) max_number
    FROM (
      SELECT [
        STRUCT(1 AS p,[STRUCT('a' AS letter, [4,5,6] AS qb)] AS qq)
        , STRUCT(2,[STRUCT('b', [7,8,9])])
        , STRUCT(3,[STRUCT('c', [10,11,12])])
       ] AS row
    )
    

    enter image description here

    还没有测试过这个方法:

    SELECT
     sum(totals.totalTransactionRevenue)/1000000 as revenue,
     (SELECT MAX(IF(index=10,value,NULL)) FROM UNNEST(hits) AS hit, UNNEST(hit.products) product,  UNNEST(product.customDimensions)) AS product_CD10,
     (SELECT MAX(IF(index=1,value,NULL)) FROM UNNEST(hits) AS hit, UNNEST(hit.customDimensions)) AS CD1
    FROM `XXXXXXXXXXXXXXX.ga_sessions_*`, 
    WHERE _TABLE_SUFFIX BETWEEN "20180608" AND "20180608"
    group by product_CD10,CD1