google bigquery SQL中的聚合或分组数据,用于捕获事件类别,收入,项目数量

时间:2017-06-02 02:29:48

标签: sql google-bigquery

如何将所有结果合并到一列?

the sql I used is as follwings:
SELECT distinct
  hits.transaction.transactionId,
  Date,
  totals.pageviews,
  hits.item.itemQuantity,
  hits.transaction.transactionRevenue,
  totals.bounces,
  fullvisitorid,
  totals.timeOnSite,
  device.browser,
  device.deviceCategory,
  trafficSource.source,
  channelGrouping,
  hits.page.pagePath,
  hits.eventInfo.eventCategory ,
  device.operatingSystem


FROM
  `atomic-life-148403.126959513.ga_sessions_*`,
  UNNEST(hits) AS hits

WHERE
  _TABLE_SUFFIX BETWEEN REPLACE(CAST(DATE_ADD(CURRENT_DATE(), INTERVAL -1 YEAR) AS STRING), '-','')
  AND CONCAT('intraday_', REPLACE(CAST(DATE_ADD(CURRENT_DATE(), INTERVAL 0 DAY) AS STRING), '-','')) 

  and date>="20170401" and date<="20170403"


  ORDER BY
  date  DESC

但是,我的结果如下: enter image description here

问题是itemquantity,transaction revenue和eventcategory不在同一行。如何解决将数据聚合或分组为一个

的问题

1 个答案:

答案 0 :(得分:0)

目前还不完全清楚您要尝试做什么,但如果目标是为每个(eventCategory, date)组合设置一行,则可以使用ARRAY_AGG(STRUCT(...))将其余列合并为一个每行的单个值。例如,

SELECT
  hits.eventInfo.eventCategory,
  date,
  ARRAY_AGG(STRUCT(
    hits.transaction.transactionId,
    hits.item.itemQuantity,
    hits.transaction.transactionRevenue,
    hits.page.pagePath,
    totals.pageviews,
    totals.bounces,
    fullvisitorid,
    totals.timeOnSite,
    device.browser,
    device.deviceCategory,
    trafficSource.source,
    channelGrouping,
    device.operatingSystem
  )) AS event_info
FROM
  `atomic-life-148403.126959513.ga_sessions_*`
CROSS JOIN UNNEST(hits) AS hits
WHERE
  _TABLE_SUFFIX BETWEEN
    FORMAT_DATE('%Y%m%d', DATE_ADD(CURRENT_DATE(), INTERVAL -1 YEAR)) AND
    FORMAT_DATE('intraday_%Y%m%d', CURRENT_DATE()) AND
  date BETWEEN "20170401" AND "20170403"
GROUP BY eventCategory, date
ORDER BY date DESC;