-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDVD_database.sql
More file actions
89 lines (81 loc) · 2.1 KB
/
DVD_database.sql
File metadata and controls
89 lines (81 loc) · 2.1 KB
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
/*Query 1.3-query used for first insight*/
WITH t1 AS
(
SELECT f.title filme_title,
c.name category_name,
f.rental_duration rental_duration,
NTILE(4)OVER(ORDER BY rental_duration) standard_quartile
FROM film f
JOIN film_category fc
ON f.film_id=fc.film_id
JOIN category c
ON c.category_id=fc.category_id
WHERE c.name IN ('Animation', 'Children', 'Classics', 'Comedy', 'Family', 'Music')
)
SELECT DISTINCT(category_name),
standard_quartile,
COUNT(*)
FROM t1
GROUP BY 1,2
ORDER BY 1,2;
/*Query 2.1-query used for second insight*/
SELECT DATE_PART('month',r.rental_date) AS Rental_month,
DATE_PART('year',r.rental_date) AS Rental_year,
st.store_id Store_ID,
COUNT(r.rental_id)
FROM rental r
JOIN staff st
ON st.staff_id=r.staff_id
GROUP BY 1,2,3
ORDER BY 4 DESC;
/*Query 2.2-query used for third insight*/
WITH t1 AS
(
SELECT customer_id cus, SUM(amount) total_amt
FROM payment
GROUP BY 1
ORDER BY total_amt DESC
LIMIT 10
)
SELECT DATE_TRUNC('month',p.payment_date) AS month,
c.first_name||' '||c.last_name full_name,
COUNT(p.amount) pay_counterpermonth,
SUM(p.amount) pay_amount
FROM customer c
JOIN payment p
ON c.customer_id=p.customer_id
JOIN t1
ON t1.cus=p.customer_id
GROUP BY 2,1
ORDER BY 2;
/*Query 2.3-query used for fourth insight*/
WITH t1 AS
(
SELECT customer_id cus,
SUM(amount) total_amt
FROM payment
GROUP BY 1
ORDER BY total_amt DESC
LIMIT 10
),
t2 AS
(
SELECT DATE_TRUNC('month',p.payment_date) AS month,
c.first_name||' '||c.last_name full_name,
SUM(p.amount) pay_amount
FROM customer c
JOIN payment p
ON c.customer_id=p.customer_id
JOIN t1
ON t1.cus=p.customer_id
GROUP BY 2,1
ORDER BY 2
)
SELECT t2.month,
t2.full_name,
t2.pay_amount,
LEAD(t2.pay_amount)OVER(PARTITION BY t2.full_name ORDER BY t2.pay_amount) AS lead,
LEAD(pay_amount)OVER(PARTITION BY t2.full_name ORDER BY t2.pay_amount)-t2.pay_amount AS lead_difference,
COALESCE(LEAD(pay_amount)OVER(PARTITION BY t2.full_name ORDER BY t2.pay_amount)-t2.pay_amount,0) AS lead_difference_adjust
FROM t2
ORDER BY 6 DESC;