sql - 如何使用基于现有列计算的 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
但我无法理解它。
有什么建议么?
干杯
解决方案
您可以使用子查询对成功事件和开始事件执行计算,然后计算最终结果。这是示例:
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
推荐阅读
- mongodb - How to $setDifference in array & Object using Mongo DB
- image - 图像的大小与其 base64 表示的大小之间是否存在直接关系?
- permissions - 更改设置后如何检测应用程序是否重新打开?
- json - mapstructure如何使用鉴别器来解码具体类型
- tensorflow - Tensorflow Estimator Early Stopping 钩子参数
- javascript - 如何使用jquery单击活动和非活动按钮后计算价格
- android-constraintlayout - 添加约束以编程方式查看
- configuration - 在 yaml 中设置文件路径位置
- reactjs - 我不明白我的 React 警告“无法在未安装的组件上调用 setstate”来自哪里
- php - PHP中str_replace的未解决问题