I'm stuck. Sum of a Windowed/Partitioned Sum???

I keep running into a wall with this. I think there is an easy answer, I just can't find it for the life of me.

  • My dataset contains one row per store, per day.
  • I want to display -- by region -- the count of stores with an average order value (AOV) less than $20.
  • In order to do that, I first need to calculate AOV by store for the period.

I'm having trouble understanding why this won't work:

SUM(
CASE WHEN (SUM(`Revenue`) / SUM(`Transactions`)) < 20 THEN 1 ELSE 0 END
) OVER (PARTITION BY `Store`)

I have also tried multiple iterations of this, to no avail:

COUNT(
CASE WHEN
SUM(SUM(`Revenue`) FIXED (BY `Store)) / SUM(SUM(`Transactions`) FIXED (BY `Store`))
< 20 THEN 1 END
) OVER (PARTITION BY `Region`)

Here is a mockup in Excel, with yellow to represent beast modes.


Answers