首页 > 解决方案 > 如何使用基于现有列计算的 SQL 在 Google BigQuery 中创建新列?

问题描述

我有一些 BigQuery 代码正在拉动在网站上开始会话的用户数量以及来自谷歌分析的成功会话数量。该代码正在创建一个临时表。

SELECT
SUM(CASE
    WHEN eventAction = 'end' AND eventLabel = 'success' THEN uniqueevents END ) AS success,
 SUM(CASE
    WHEN eventAction = 'begin' AND eventLabel = 'begin' THEN uniqueevents END ) AS starts

我需要在我的临时表的新列中计算成功率(成功/开始)。我已经阅读了几篇建议使用的帖子,group by但我无法理解它。

有什么建议么?

干杯

标签: sqlgoogle-bigquery

解决方案


您可以使用子查询对成功事件和开始事件执行计算,然后计算最终结果。这是示例:

WITH
  sample AS (
  SELECT
    'end' AS eventAction,
    'success' AS eventLabel,
    10 AS uniqueevents
  UNION ALL
  SELECT
    'begin',
    'begin',
    20 ),
  pre_cal AS (
  SELECT
    SUM(CASE
        WHEN eventAction = 'end' AND eventLabel = 'success' THEN uniqueevents
    END
      ) AS success,
    SUM(CASE
        WHEN eventAction = 'begin' AND eventLabel = 'begin' THEN uniqueevents
    END
      ) AS starts
  FROM
    sample )
SELECT
  success,
  starts,
  success/starts AS per
FROM
  pre_cal

推荐阅读