…
…
Rank eight branches by a year of revenue with window functions, compare month to month, compute a running total and a moving average, and save a branch scorecard as a view.
Corner Café has grown: there are eight branches in three regions of Port Alder. For the year-end meeting the general manager wants a performance report built from each branch's revenue over the last 12 months (December 2025 – November 2026): what rank each branch has in its region, who the star of each region is, how the biggest branch changed from month to month, how the chain grew through the year, and a scorecard for every branch.
Data: branch (id, name, region) and monthly_revenue (branch_id, month_start, revenue). month_start is the first day of the month (for example 2026-03-01); revenue is a whole number of dollars. The values are fictional sample data.
Lessons used: CTE (23), date formatting (27), views (38), window functions: ranking (41), LAG, running totals and frames (42).
In the report you will turn the month column into text with TO_CHAR(month_start, 'YYYY-MM'), so it reads like "2026-03".
What you'll do
branches.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
First you will calculate each branch's yearly total revenue and find its rank in its own region. Because a window function runs after GROUP BY, you can use SUM(r.revenue) in the ordering of RANK().
For each branch show region, name, the yearly revenue (total) and the rank in its region (region_rank, RANK, largest first). Sort by region, then rank.
This step's code cell is for members.Sign up free
You want to show only the first-ranked branch of each region. The result of a window function cannot be used in WHERE (lesson 41); first calculate the totals and the rank in a CTE, then filter in the outer query.
The totals CTE already gives each branch's yearly revenue. Show the region, name and total
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
This step's code cell is for members.Sign up free
The biggest branch of the chain is Harbor (branch_id 1). You will see how much its revenue changed each month compared with the month before. LAG brings the value of the previous row in the window order; in the first month there is nothing before it, so it returns NULL.
For Harbor show each month (month, 'YYYY-MM'), its revenue, the difference from the previous month (change) and the percentage change (pct, one decimal). Sort by month.
This step's code cell is for members.Sign up free
The general manager wants to see the revenue that has built up through the year. First calculate the chain total of each month in a CTE, then add the running total in the outer query with SUM(...) OVER (ORDER BY month_start). If you do not write ORDER BY inside OVER, every row gets the yearly total.
For each month show month ('YYYY-MM'), the chain's revenue that month (revenue) and the total up to that month (running). Sort by month.
This step's code cell is for members.Sign up free
Monthly revenue swings from season to season. To see the trend, you will calculate for every month the average of that month and the two months before it. You narrow the frame with ROWS BETWEEN 2 PRECEDING AND CURRENT ROW; in the first two months the rows that exist are enough.
For each month show month ('YYYY-MM'), the chain revenue (revenue) and the three-month moving average (avg_3m, rounded to a whole number). Sort by month.
This step's code cell is for members.Sign up free
In the meeting each branch's one-line scorecard will be shown, and it will be reused every month; so you will save it as a view. The scorecard has the branch's yearly revenue, its share of its region's revenue and its rank in the whole chain. The denominator of the share is the regional total of the grouped sums: SUM(SUM(r.revenue)) OVER (PARTITION BY b.region). This is the same pattern as SUM(COUNT(*)) OVER () in lesson 41: the window runs after GROUP BY, and because it is partitioned by region, each branch gets the total of its own region next to it.
You write the view's three calculated columns: total, region_share (one decimal) and chain_rank (RANK by yearly revenue, largest first). The cell lists the view in rank order.
This step's code cell is for members.Sign up free