【问题标题】:How to calculate percentage increase between rows in Oracle SQL?如何计算 Oracle SQL 中行之间的百分比增加?
【发布时间】:2021-01-15 14:26:47
【问题描述】:

我有如下数据:

YEAR_MONTH|AVG_VISITS|WAS_MEMBER
2020-09|10|True
2020-09|5|False
2019-04|2.5|True
2019-04|5|False

我想把它做成一个表格来计算增加的​​访问会员的百分比:

YEAR_MONTH|VISIT_PERCENT
2020-09|200
2019-04|50

什么 SQL 可以让我在行之间进行这种计算?

【问题讨论】:

    标签: sql oracle percentage


    【解决方案1】:

    你只需要条件聚合如下:

    select year_month,
           100 * sum(case when WAS_MEMBER = 'True' then avg_visits end) /
           sum(case when WAS_MEMBER = 'False' then avg_visits end) as perc_increase
      from your_table t
    group by year_month
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 2021-06-30
      相关资源
      最近更新 更多