Overview
Show each category's share of revenue with SUM() OVER (), and avoid the integer division that turns every percentage into 0.
- SUM() OVER () is the grand total
- Integer division truncates
- 100.0 * part / total
Short Course
SUM(x) OVER () puts the grand total on every row, so each row can show its
share. Trap: dividing two integers gives an integer in SQL, so 30 / 40 is
0. Multiply by 100.0 first: 100.0 * 30 / 40 is 75.0.
SELECT team, wins,
ROUND(100.0 * wins / SUM(wins) OVER (), 1) AS pct
FROM standings;
Task
Return each category with its revenue (SUM(amount)) and its percentage of
total revenue rounded to 1 decimal, highest revenue first. In the database
below, books make 40 of 100, so 40%.
Database
Sample data from the first test. The other tests use different rows, so write a query, not a list of answers.
| idINTEGER | categoryTEXT | amountINTEGER |
|---|---|---|
| 1 | books | 30 |
| 2 | games | 50 |
| 3 | books | 10 |
| 4 | toys | 10 |
Test Suites
Pending
Next
Output