在BigQuery

时间:2018-01-10 10:46:10

标签: sql google-bigquery

我有一个查询从BigQuery中的一个数据集收集动态日期范围(过去7天)的数据 - 我的数据源是Google Analytics,因此我有其他数据集连接相同的架构。我希望我的查询也返回其他数据集中的数据,通常我会使用UNION ALL,但是我的查询包含一个复杂的分类查询,需要定期更新,我不想多次为每个集。

您能否就如何跨数据集进行查询或建议更优雅的方式来处理UNION ALL方法?

SELECT
  Date,
  COUNT(DISTINCT VisitId) AS users,
  COUNT(VisitId) AS sessions,
  SUM(totals.transactions) AS orders,
  CASE
  # Organic Search - Google
    WHEN ( channelGrouping LIKE "Organic Search"
    OR trafficSource.source LIKE "com.google.android.googlequicksearchbox")
  AND trafficSource.source LIKE "%google%" THEN "Organic Search - Google"
    ELSE "Other"
  END AS Channel,
  hits.page.hostname AS site
FROM
  `xxx.dataset1.ga_sessions_20*`
CROSS JOIN
  UNNEST (hits) AS hits
WHERE
  parse_DATE('%y%m%d',
    _table_suffix) BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 7 day)
  AND DATE_SUB(CURRENT_DATE(), INTERVAL 1 day)
AND totals.visits = 1
AND hits.isEntrance IS TRUE
GROUP BY
  Date,
  Channel,
  hits.isEntrance
ORDER BY
  Users DESC

更新:由于下面的回复,我已经得到了以下内容,以下查询UNION中的所有数据集但是日期范围没有应用,而是所有数据都在查询,任何想法为什么它没有提起它?

    SELECT
      Date,
      LOWER(hits.page.hostname) AS site,
      IFNULL(COUNT(VisitId),0) AS sessions,
      IFNULL(SUM(totals.transactions),0) AS orders,
      IFNULL(ROUND(SUM(totals.transactions)/COUNT(VisitId),4),0) AS conv_rate,
    # Channel definition starts here
        CASE
  # Organic Search - Google
    WHEN ( channelGrouping LIKE "Organic Search"
    OR trafficSource.source LIKE "com.google.android.googlequicksearchbox")
  AND trafficSource.source LIKE "%google%" THEN "Organic Search - Google"
    ELSE "Other"
  END AS Channel
    FROM (
      SELECT * FROM `xxx.43786551.ga_sessions_20*` UNION ALL
      SELECT * FROM `xxx.43786097.ga_sessions_20*` UNION ALL
      SELECT * FROM `xxx.43786092.ga_sessions_20*` 
      WHERE PARSE_DATE('%Y%m%d',_TABLE_SUFFIX) BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)
      AND DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
    )
    CROSS JOIN UNNEST (hits) AS hits
    WHERE totals.visits = 1
    AND hits.isEntrance IS TRUE
    GROUP BY
      Date,
      channel,
      hits.isEntrance,
      site
    HAVING hits.isEntrance IS TRUE

1 个答案:

答案 0 :(得分:3)

#standardSQL
SELECT
  DATE,
  COUNT(DISTINCT VisitId) AS users,
  COUNT(VisitId) AS sessions,
  SUM(totals.transactions) AS orders,
  CASE
  # Organic Search - Google
    WHEN ( channelGrouping LIKE "Organic Search"
    OR trafficSource.source LIKE "com.google.android.googlequicksearchbox")
  AND trafficSource.source LIKE "%google%" THEN "Organic Search - Google"
    ELSE "Other"
  END AS Channel,
  hits.page.hostname AS site
FROM (
  SELECT * FROM `xxx.dataset1.ga_sessions_20*` WHERE PARSE_DATE('%y%m%d',_TABLE_SUFFIX) BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY) AND DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
  UNION ALL SELECT * FROM `xxx.dataset2.ga_sessions_20*` WHERE PARSE_DATE('%y%m%d',_TABLE_SUFFIX) BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY) AND DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
  UNION ALL SELECT * FROM `xxx.dataset3.ga_sessions_20*` WHERE PARSE_DATE('%y%m%d',_TABLE_SUFFIX) BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY) AND DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)
)
CROSS JOIN UNNEST (hits) AS hits
WHERE totals.visits = 1
AND hits.isEntrance IS TRUE
GROUP BY
  DATE,
  Channel,
  site
ORDER BY
  Users DESC