-
Notifications
You must be signed in to change notification settings - Fork 0
/
queriesDW.sql
105 lines (100 loc) · 4.29 KB
/
queriesDW.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* 1. Which product generated maximum sales in September, 2017? */
SELECT * FROM ( SELECT D_PRODUCTS.PRODUCT_NAME AS "PRODUCT NAME",
SUM(CASE WHEN D_TIME.CAL_MONTH = 'SEP'
THEN W_FACTS.SALE END) AS "SALE",
RANK() OVER
(ORDER BY SUM(CASE WHEN D_TIME.CAL_MONTH = 'SEP'
THEN W_FACTS.SALE END) DESC)
AS "RANK"
FROM W_FACTS , D_TIME, D_PRODUCTS
WHERE W_FACTS.TIME_ID = D_TIME.TIME_ID
AND W_FACTS.PRODUCT_ID = D_PRODUCTS.PRODUCT_ID
GROUP BY D_PRODUCTS.PRODUCT_NAME
ORDER BY "RANK"
)
WHERE "RANK"=1
;
/* 2. Determine top three supplier names based on highest product sales. */
SELECT * FROM ( SELECT RANK() OVER (ORDER BY SUM(W_FACTS.SALE) DESC) AS "RANK",
D_SUPPLIERS.SUPPLIER_NAME AS "SUPPLIER NAME",
SUM(W_FACTS.SALE) AS "SALE"
FROM W_FACTS, D_SUPPLIERS
WHERE W_FACTS.SUPPLIER_ID = D_SUPPLIERS.SUPPLIER_ID
GROUP BY W_FACTS.SUPPLIER_ID, D_SUPPLIERS.SUPPLIER_NAME
ORDER BY "RANK"
)
WHERE "RANK"<4
;
/* 3. Determine the top 3 stores who generated highest sales in SEP 2017. */
SELECT * FROM ( SELECT RANK() OVER
(ORDER BY SUM(CASE WHEN D_TIME.CAL_MONTH = 'SEP'
THEN W_FACTS.SALE END) DESC) AS "RANK",
D_STORES.STORE_NAME AS "STORE NAME",
SUM(CASE WHEN D_TIME.CAL_MONTH = 'SEP'
THEN W_FACTS.SALE END) AS "SALE"
FROM W_FACTS, D_TIME, D_STORES
WHERE W_FACTS.TIME_ID = D_TIME.TIME_ID
AND W_FACTS.STORE_ID = D_STORES.STORE_ID
GROUP BY D_STORES.STORE_NAME
ORDER BY "RANK"
)
WHERE "RANK"<4
;
/* 4. Presents the quarterly sales analysis for all stores using drill down
query concepts. */
SELECT STORE_NAME AS "STORE NAME",
SUM(CASE WHEN Q=1 THEN TOTAL END) AS "Q1_2017",
SUM(CASE WHEN Q=2 THEN TOTAL END) AS "Q2_2017",
SUM(CASE WHEN Q=3 THEN TOTAL END) AS "Q3_2017",
SUM(CASE WHEN Q=4 THEN TOTAL END) AS "Q4_2017"
FROM
( SELECT D_STORES.STORE_NAME, D_TIME.CAL_QUARTER AS Q,
SUM(W_FACTS.SALE) AS TOTAL
FROM W_FACTS, D_STORES, D_TIME
WHERE W_FACTS.STORE_ID = D_STORES.STORE_ID
AND W_FACTS.TIME_ID = D_TIME.TIME_ID
GROUP BY D_STORES.STORE_NAME, D_TIME.CAL_QUARTER
)
GROUP BY STORE_NAME
ORDER BY STORE_NAME
;
/* 5. Create a materialised view with name “STORE_PRODUCT_ANALYSIS” that
presents store and product wise sales.
The results should be ordered by store name and then product name. */
-- Drop any existing MV
DROP materialized VIEW STORE_PRODUCT_ANALYSIS;
-- Create MV
CREATE materialized VIEW STORE_PRODUCT_ANALYSIS
AS
SELECT D_STORES.STORE_NAME AS "STORE NAME",
D_PRODUCTS.PRODUCT_NAME AS "PRODUCT NAME",
SUM(W_FACTS.SALE) AS "SALE"
FROM W_FACTS, D_STORES, D_PRODUCTS
WHERE W_FACTS.STORE_ID = D_STORES.STORE_ID
AND W_FACTS.PRODUCT_ID = D_PRODUCTS.PRODUCT_ID
GROUP BY D_STORES.STORE_NAME, D_PRODUCTS.PRODUCT_NAME
;
-- Display MV
SELECT * FROM STORE_PRODUCT_ANALYSIS
ORDER BY "STORE NAME" ASC, "PRODUCT NAME" ASC
;
/* 6. Create a materialised view with name “MONTH_STORE_ANALYSIS” that presents
month and store wise sales.
The results should be ordered by month name and then store name. */
-- Drop any existing MV
DROP materialized VIEW MONTH_STORE_ANALYSIS;
-- Create MV
CREATE materialized VIEW MONTH_STORE_ANALYSIS
AS
SELECT D_TIME.CAL_MONTH AS "MONTH",
D_STORES.STORE_NAME AS "STORE NAME",
SUM(W_FACTS.SALE) AS "SALE"
FROM W_FACTS, D_STORES, D_TIME
WHERE W_FACTS.STORE_ID = D_STORES.STORE_ID
AND W_FACTS.TIME_ID = D_TIME.TIME_ID
GROUP BY D_STORES.STORE_NAME, D_TIME.CAL_MONTH
;
-- Display MV
SELECT * FROM MONTH_STORE_ANALYSIS
ORDER BY "MONTH" ASC, "STORE NAME" ASC
;