-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL_Query-1.sql
28 lines (27 loc) · 950 Bytes
/
SQL_Query-1.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/* Analyze the yearly performance of products by comparing their sales
to both the average sales performance of the product and the previous year's sales */
WITH yearly_product_sales as
(
select
YEAR(f.order_date) AS ORDER_YEAR,
p.product_name,
SUM(f.sales_amount) AS CURRENT_SALES
FROM gold.fact_sales f
LEFT JOIN gold.dim_products p
ON f.product_key=p.product_key
WHERE f.order_date is not null
GROUP BY
YEAR(f.order_date),p.product_name
)
SELECT
order_year,
product_name,
CURRENT_SALES,
AVG(current_sales) OVER (PARTITION BY product_name) AS AVG_SALES,
current_sales - AVG(current_sales) OVER (PARTITION BY product_name) AS DIFF_AVG,
CASE WHEN current_sales - AVG(current_sales) OVER (PARTITION BY product_name) > 0 THEN 'Above Avg'
WHEN current_sales - AVG(current_sales) OVER (PARTITION BY product_name) < 0 THEN 'Below Avg'
ELSE 'AVG'
END Avg_change
FROM yearly_product_sales
ORDER BY product_name,order_year