【问题标题】:BigQuery - How to calculate percentage change row by row?BigQuery - 如何逐行计算百分比变化?
【发布时间】:2021-11-29 21:54:43
【问题描述】:

我在我的 Android 应用中实现了标签来跟踪用户的行为。每个按钮都注册为特定事件。考虑到这一点,数据显示在如下表中:

| event         |event_count| 
| home button   |  10000    | 
| menu button   |  6265     | 
| Log in button |  5248     | 
|Product1 button|  3230     | 
|Product2 button|  1892     | 
|Payment button |  643      | 

事件计数是每个按钮的点击次数。

我想在 BigQuery 中创建一列来计算每个事件之间的百分比变化(当前行的 event_count - 上一行的 event_count/上一行的 event_count):

| event         |event_count| % change
| home button   |  10000    | 0
| menu button   |  6265     | -37,35%
| Log in button |  5248     | -16,23%
|Product1 button|  3230     | -38,45%
|Product2 button|  1892     | -41,42%
|Payment button |  643      | -66% 

请问,您知道如何在 BigQuery 的标准 SQL 中执行此操作吗?另外,如果可能,我需要在百分比变化为负时出现负号。

表格可以这样排序:

ORDER BY event_count DESC

获得更多点击的事件将位于顶部。由于旅程是线性的,因此特定行中的事件通常会比前一个事件获得更多点击。

我的目标是找出我失去更多用户的地方,以改善他们的体验。

【问题讨论】:

  • 识别哪一行是前一行——你需要知道考虑行的顺序——那么定义行的顺序是什么?
  • @MikhailBerlyant 该表可以这样排序:ORDER BY event_count DESC。获得更多点击的事件将位于顶部。由于旅程是线性的,因此特定行中的事件通常会获得比前一个事件更多的点击次数。

标签: sql google-bigquery


【解决方案1】:

考虑以下方法

select *, 
  round(ifnull(100 * (event_count/lag(event_count) over(order by event_count desc) - 1), 0), 2) || '%'
from your_table

如果应用于您问题中的样本数据 - 输出是

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 2016-02-22
    • 2022-11-19
    • 2015-09-07
    • 2019-01-26
    相关资源
    最近更新 更多