Example 1
Show each category as a share of total sales
The total sales amount is 200. Music contributes 100 of 200, or 50%. Games contributes 30%, and Books contributes 20%. The window function keeps each original row while making the total available to every row.
CREATE TABLE sales (category VARCHAR(20), amount INT);
INSERT INTO
sales (category, amount)
VALUES
('Books', 40),
('Games', 60),
('Music', 100);SELECT
category,
amount,
ROUND(amount * 100.0 / SUM(amount) OVER (), 1) AS pct_of_total
FROM
sales
ORDER BY
amount DESC;| category | amount | pct_of_total |
|---|---|---|
| Music | 100 | 50 |
| Games | 60 | 30 |
| Books | 40 | 20 |
All supported engines use the same window-function pattern here and return the same percentages.