如何计算 Oracle SQL 中行之间的百分比增加?
How to calculate percentage increase between rows in Oracle SQL?
我有这样的数据:
YEAR_MONTH|AVG_VISITS|WAS_MEMBER
2020-09|10|True
2020-09|5|False
2019-04|2.5|True
2019-04|5|False
我想把它变成一个 table 来计算添加的访问成员的百分比:
YEAR_MONTH|VISIT_PERCENT
2020-09|200
2019-04|50
什么 SQL 让我在行之间查找此类计算?
您只需要如下条件聚合:
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
我有这样的数据:
YEAR_MONTH|AVG_VISITS|WAS_MEMBER
2020-09|10|True
2020-09|5|False
2019-04|2.5|True
2019-04|5|False
我想把它变成一个 table 来计算添加的访问成员的百分比:
YEAR_MONTH|VISIT_PERCENT
2020-09|200
2019-04|50
什么 SQL 让我在行之间查找此类计算?
您只需要如下条件聚合:
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