No. | Title |
---|---|
1 | Explanation |
2 | Coding Example |
3 | Case Studies |
4 | Conclusion |
SELECT
product_name,
region,
date,
quantity_sold,
SUM(quantity_sold) OVER (PARTITION BY product_name, region ORDER BY date) AS cumulative_quantity
FROM
sales;
-- Find the top 3 regions with the highest sales for each product
SELECT
product_name,
region,
sales_amount
FROM (
SELECT
product_name,
region,
sales_amount,
ROW_NUMBER() OVER (PARTITION BY product_name ORDER BY sales_amount DESC) AS rn
FROM
sales
) AS ranked_sales
WHERE rn <= 3;
SELECT
customer_id,
SUM(total_amount) AS total_order_amount
FROM
orders
GROUP BY
customer_id;
SELECT
order_id,
customer_id,
order_date,
total_amount,
(total_amount / SUM(total_amount) OVER (PARTITION BY customer_id)) * 100 AS percentage_contribution
FROM
orders;
WITH customer_orders AS (
SELECT
customer_id,
SUM(total_amount) AS total_order_amount
FROM
orders
GROUP BY
customer_id
)
SELECT
o.order_id,
o.customer_id,
o.order_date,
o.total_amount,
(o.total_amount / co.total_order_amount) * 100 AS percentage_contribution
FROM
orders o
JOIN
customer_orders co ON o.customer_id = co.customer_id;