RUNNING_SUM(SUM([Sales])) / WINDOW_SUM(SUM([Sales])) is the share of the region's sales reached by each month, ending at 1. In SQL the numerator is a running window and the denominator a whole-partition one: sum(sum(Sales)) OVER (PARTITION BY Region ORDER BY Month) / sum(sum(Sales)) OVER (PARTITION BY Region).
SELECT Region,
Month,
sum(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month)
/sum(sum(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Compute using Month: East reaches 0.25, 0.55, 0.70 and 1. West holds at 0.57 through its null month.
Region
Month
Result
East
1
0.25
East
2
0.55
East
3
0.7
East
4
1
West
1
0.24
West
2
0.57
West
3
0.57
West
4
1
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why is every month 1 in the SQL?
Using the same running window for both parts divides the running total by itself. WINDOW_SUM has no start and end, so it covers the whole region: drop ORDER BY from the denominator.
SELECT Region,
Month,
sum(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month)
/sum(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SQL that matches
SELECT Region,
Month,
sum(sum(Sales)) OVER (PARTITIONBY Region ORDERBY Month)
/sum(sum(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
Region
Month
Tableau
Tempting SQL
SQL that matches
East
1
0.25
1
0.25
East
2
0.55
1
0.55
East
3
0.7
1
0.7
East
4
1
1
1
West
1
0.24
1
0.24
West
2
0.57
1
0.57
West
3
0.57
1
0.57
West
4
1
1
1
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
How do I write it in Snowflake or BigQuery?
The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.
Snowflake
SELECT Region,
Month,
SUM(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month)
/SUM(SUM(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
SELECT Region,
Month,
SUM(SUM(Sales)) OVER (PARTITIONBY Region ORDERBY Month)
/SUM(SUM(Sales)) OVER (PARTITIONBY Region) AS result
FROM input
GROUPBY Region, Month
ORDERBY Region, Month
What do people ask about Running percent of total?
Why does the expression need SUM inside it?
A table calculation runs on the marks in the view, so its argument must be an aggregate. A bare field such as [Sales] raises an error; see All fields must be aggregate or constant.
Why no ORDER BY in the SQL?
Without start and end, a WINDOW_ function covers the whole partition. A SQL window with ORDER BY and no frame ends at the current row, so leave ORDER BY out, or write ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
Is this the Running Total, then Percent of Total quick calculation?
It gives the same numbers as a running total with a secondary percent-of-total calculation along the same direction.