Complete the code to calculate the percent of total sales for each product.
SELECT product_id, sales_amount, sales_amount [1] SUM(sales_amount) OVER () AS percent_of_total FROM sales;We divide each sales_amount by the total sales (SUM over all rows) to get the percent of total.
Complete the code to calculate the percent of total sales rounded to two decimal places.
SELECT product_id, ROUND(sales_amount [1] SUM(sales_amount) OVER () * 100, 2) AS percent_of_total FROM sales;
We divide sales_amount by total sales, multiply by 100 to get a percentage, then round to 2 decimals.
Fix the error in the code to correctly calculate the percent of total sales per category.
SELECT category, sales_amount, sales_amount [1] SUM(sales_amount) OVER (PARTITION BY category) AS percent_of_category_total FROM sales;To get the percent of total per category, divide sales_amount by the sum of sales_amount partitioned by category.
Fill both blanks to calculate the percent of total sales and alias the result as percent_total.
SELECT product_id, sales_amount, (sales_amount [1] SUM(sales_amount) OVER ()) * 100 AS [2] FROM sales;
Divide sales_amount by total sales, multiply by 100, and name the column percent_total.
Fill all three blanks to calculate the percent of total sales per region, rounded to 1 decimal place, and alias it as pct_region.
SELECT region, sales_amount, ROUND((sales_amount [1] SUM(sales_amount) OVER (PARTITION BY region)) * [2], [3]) AS pct_region FROM sales;
Divide sales_amount by total sales per region, multiply by 100 to get percent, then round to 1 decimal place.