Percent of total revenue per category
MediumP2Coding
Table sales:
| Column | Type |
|---|---|
| id | int (primary key) |
| category | text |
| amount | int (positive) |
Return each category's share of total revenue, in percent, rounded to 2 decimals.
Output columns: category, pct.
Schema
CREATE TABLE sales ( id int PRIMARY KEY, category text NOT NULL, amount int NOT NULL CHECK (amount > 0) );
Example 1
- Input
- sales: (1, a, 50), (2, b, 30), (3, c, 20)
- Output
- a: 50.00, b: 30.00, c: 20.00
Example 2
- Input
- sales: (1, a, 1), (2, b, 1), (3, c, 1)
- Output
- 33.33 for each category
Constraints
- PostgreSQL dialect.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
PostgreSQL
Run checks the 3 visible tests, Submit all 6. ⌘Enter runs, ⌘⇧Enter submits.