-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.sql
65 lines (59 loc) · 2.03 KB
/
script.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
-- Create the "customers" table
CREATE TABLE customers (
customer_id SERIAL PRIMARY KEY,
customer_name VARCHAR(255) NOT NULL,
email_address VARCHAR(255),
country VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- track when the customer was added
) DISTRIBUTED BY (customer_id);
-- Create the "products" table
CREATE TABLE products (
product_id SERIAL PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
category VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- track when the product was added
) DISTRIBUTED BY (product_id);
-- Create the "sales_transactions" table
CREATE TABLE sales_transactions (
transaction_id SERIAL PRIMARY KEY,
customer_id INT REFERENCES customers(customer_id) ON DELETE CASCADE,
product_id INT REFERENCES products(product_id) ON DELETE CASCADE,
purchase_date DATE NOT NULL,
quantity INT NOT NULL
) DISTRIBUTED BY (transaction_id);
-- Create the "shipping_details" table
CREATE TABLE shipping_details (
transaction_id INT PRIMARY KEY REFERENCES sales_transactions(transaction_id) ON DELETE CASCADE, -- foreign key and primary key
shipping_date DATE NOT NULL,
shipping_address VARCHAR(255) NOT NULL,
city VARCHAR(100),
country VARCHAR(100)
) DISTRIBUTED BY (transaction_id);
-- 2. SQL Query for Analysis
WITH monthly_sales AS (
SELECT
DATE_TRUNC('month', st.purchase_date) AS sales_month,
SUM(p.price * st.quantity) AS total_amount,
SUM(st.quantity) as total_qty
FROM
sales_transactions st
INNER JOIN
products p ON p.product_id = st.product_id
GROUP BY
DATE_TRUNC('month', st.purchase_date)
ORDER BY
sales_month DESC
)
SELECT
sales_month,
total_amount,
total_qty,
AVG(total_amount) OVER (
ORDER BY sales_month DESC
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) AS moving_avg_3_month_sales
FROM
monthly_sales
ORDER BY
sales_month DESC;