Поскольку сеансы были определены на разных уровнях, код не работал.Однако, когда я сделал 2 отдельные таблицы и соединил их, это работало просто отлично.Эти две таблицы имели разные маршруты для получения сеансов, которые обеспечивали правильную работу вычислений.
SELECT
actiontimestamp,
medium,
source,
sessions,
ROUND(SAFE_DIVIDE(pageviews,
sessions), 0) AS pages_per_session,
CASE
WHEN sessions = 0 THEN 0
ELSE ROUND(SAFE_DIVIDE(bounces,
sessions), 2)
END AS bounce_rate,
ROUND(avgTimeOnSite, 2) AS avgTimeOnSite
FROM (
SELECT
actiontimestamp,
medium,
source,
AVG(pageviews) AS pageviews,
SUM(bounces) AS bounces,
SUM(sessions) AS sessions,
AVG(avgTimeOnSite) AS avgTimeOnSite
FROM (
SELECT
fullVisitorId,
pageviews,
actiontimestamp,
avgTimeOnSite,
medium,
source,
CASE
WHEN hitNumber = first_interaction THEN bounces
ELSE 0
END AS bounces,
CASE
WHEN hitNumber = first_hit THEN visits
ELSE 0
END AS sessions
FROM (
SELECT
fullVisitorId,
visitStartTime,
IFNULL(totals.pageviews,
0) AS pageviews,
totals.bounces,
totals.visits,
totals.newVisits AS newVisits,
hits.hitNumber,
MIN(IF(hits.isInteraction IS NOT NULL,
hits.hitNumber,
0)) OVER (PARTITION BY fullVisitorId, visitStartTime) AS first_interaction,
MIN(hits.hitNumber) OVER (PARTITION BY fullVisitorId, visitStartTime) AS first_hit,
FORMAT_TIMESTAMP("%Y-%m-%d", TIMESTAMP_SECONDS(SAFE_CAST(visitStartTime AS INT64)), "Europe/London") AS actiontimestamp,
totals.timeOnSite AS avgTimeOnSite,
trafficSource.medium AS medium,
trafficSource.source AS source
FROM
`gatable.ga_sessions_*`,
UNNEST(hits) AS hits
WHERE
_TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', '2018-11-01')
AND FORMAT_DATE('%Y%m%d', '2018-11-30')))
GROUP BY
actiontimestamp,
medium,
source)
ORDER BY
actiontimestamp DESC
И тогда вторая таблица будет иметь вид:
SELECT
actiontimestamp,
medium,
source,
users,
newUsers,
sessions,
transactions,
ROUND((SAFE_DIVIDE(transactions,
sessions)*100), 2) AS conversion_rate
FROM (
SELECT
FORMAT_TIMESTAMP("%Y-%m-%d", TIMESTAMP_SECONDS(SAFE_CAST(visitStartTime AS INT64)), "Europe/London") AS actiontimestamp,
SUM(totals.transactions) AS transactions,
COUNT(DISTINCT fullVisitorId) AS users,
SUM(totals.visits) AS sessions,
COUNT(totals.newVisits) AS newUsers,
trafficSource.medium AS medium,
trafficSource.source AS source
FROM
`91775944.ga_sessions_*`
WHERE
_TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', '2018-11-01')
AND FORMAT_DATE('%Y%m%d', '2018-11-30')
GROUP BY
actiontimestamp,
medium,
source
)
Эти таблицы затем объединяются по метке времени действия, среда и источник, и я получил результаты, которые мне были нужны.