0

I have data like:

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

I'd like to make it into a table that calculates the percentage of visits membership added:

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

What is the SQL that would let me look between rows for this sort of calculation?

Mark McGown
  • 975
  • 1
  • 10
  • 26

1 Answers1

1

You just need conditional aggregation as follows:

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
Popeye
  • 35,427
  • 4
  • 10
  • 31