change view ga_realtime_sessions_view_YYYYMMDD to standard sql

杀马特。学长 韩版系。学妹 提交于 2019-12-08 02:06:16

问题


I activated the streaming export from Google Analytics to BigQuery about a year ago but I'm having troubles changing the SQL syntax of the real-time view (ga_realtime_sessions_view_YYYYMMDD) to STANDARD SQL.

How can I change the SQL of this view? The view is defined as:

SELECT *
FROM [XXX.ga_realtime_sessions_20180424]
where exportKey in (
  SELECT exportKey
  FROM (
    SELECT
      exportKey,
      exportTimeUsec,
      MAX(exportTimeUsec) OVER (PARTITION BY visitKey) AS maxexportTimeUsec
    FROM [XXX.ga_realtime_sessions_20180424]
  )
  WHERE exportTimeUsec >= maxexportTimeUsec
);

回答1:


You can create a standard SQL view of this form:

CREATE VIEW `XXX.ga_realtime_view` AS
SELECT
  _TABLE_SUFFIX AS suffix,
  ARRAY_AGG(t ORDER BY exportTimeUsec DESC LIMIT 1)[OFFSET(0)].*
FROM `XXX.ga_realtime_sessions_20*` AS t
GROUP BY suffix, visitKey;

This returns the latest row in accordance with exportTimeUsec for each visitKey. When querying the view, filter on the suffix corresponding to the date that you want. For example,

SELECT *
FROM `XXX.ga_realtime_view`
WHERE suffix = '180424';

This returns data from the XXX.ga_realtime_sessions_20180424 table.



来源:https://stackoverflow.com/questions/49997359/change-view-ga-realtime-sessions-view-yyyymmdd-to-standard-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!