-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04. Annual Payment Type Usage Analysis.sql
104 lines (79 loc) · 3.62 KB
/
04. Annual Payment Type Usage Analysis.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
-- ====================== Annual Payment Type Usage Analysis ======================
-- Created by:
-- Nur Imam Masri
-- Email : nurimammasri.01@gmail.com
-- LinkedIn : linkedin.com/in/nurimammasri
-- Github : github.com/nurimammasri
-- Portfolio : bit.ly/ImamProjectPortfolio
-- -----------------------------------------------------------------------------------------
-- Menganalisis tipe-tipe pembayaran yang tersedia dan melihat tren perubahan yang terjadi selama beberapa tahun terakhir.
-- Langkah - langkah :
-- 1. Menampilkan jumlah penggunaan masing-masing tipe pembayaran secara all time diurutkan dari yang terfavorit
-- Hint: Perhatikan struktur (kolom-kolom apa saja) dari tabel akhir yang ingin didapatkan
-- 2. Menampilkan detail informasi jumlah penggunaan masing-masing tipe pembayaran untuk setiap tahun
-- Hint: Perhatikan struktur (kolom-kolom apa saja) dari tabel akhir yang ingin didapatkan
-- Resource :
-- https://www.postgresqltutorial.com/postgresql-case/
-- Hasil :
-- 1. Jumlah penggunaan masing-masing jenis pembayaran all time
-- 2. Detail jumlah penggunaan masing-masing jenis pembayaran untuk setiap tahun
-- 3. Satu tabel summary jumlah penggunaan tipe-tipe pembayaran untuk masing-masing tahun.
-- 1. Menampilkan jumlah penggunaan masing-masing tipe pembayaran secara all time diurutkan dari yang terfavorit
-- Hint: Perhatikan struktur (kolom-kolom apa saja) dari tabel akhir yang ingin didapatkan
-- - Create a table that shows the amount of payment usage for each type of payment for all time
-- - Sort data number of usage from the largest to the smallest payment type
SELECT
op.payment_type,
count(*) AS num_of_usage
FROM orders o
JOIN order_payments op
ON o.order_id = op.order_id
GROUP BY 1
ORDER BY 2 DESC;
-- 2. Menampilkan detail informasi jumlah penggunaan masing-masing tipe pembayaran untuk setiap tahun
-- Hint: Perhatikan struktur (kolom-kolom apa saja) dari tabel akhir yang ingin didapatkan
-- - Create a table that shows the amount of payment usage for each type of payment for each year
-- - Sort data number of usage from the largest to the smallest payment type
SELECT
date_part('year', o.order_purchase_timestamp) as year,
op.payment_type,
count(*) AS num_of_usage
FROM orders o
JOIN order_payments op
ON o.order_id = op.order_id
GROUP BY 1,2
ORDER BY 1 ASC, 3 DESC;
-- 3. Satu tabel summary jumlah penggunaan tipe-tipe pembayaran untuk masing-masing tahun.
-- - Utilize the CASE WHEN function to pivot data (rows : payment_type, columns : year)
-- - Combining CASE WHEN into an aggregate function with year value windowing for dividing values in the column
-- - Sort data number of usage from the largest to the smallest payment type
-- Cara 1
WITH type_payments AS (
SELECT
date_part('year', o.order_purchase_timestamp) as year,
op.payment_type,
count(*) AS num_of_usage
FROM orders o
JOIN order_payments op
ON o.order_id = op.order_id
GROUP BY 1,2
)
select
payment_type,
sum(case when year = '2016' then num_of_usage else 0 end) as year_2016,
sum(case when year = '2017' then num_of_usage else 0 end) as year_2017,
sum(case when year = '2018' then num_of_usage else 0 end) as year_2018
from type_payments
GROUP BY 1
ORDER BY 4 DESC;
-- Cara 2
SELECT
payment_type,
count(CASE WHEN date_part('year', order_purchase_timestamp) = '2016' THEN o.order_id END) AS year_2016,
count(CASE WHEN date_part('year', order_purchase_timestamp) = '2017' THEN o.order_id END) AS year_2017,
count(CASE WHEN date_part('year', order_purchase_timestamp) = '2018' THEN o.order_id END) AS year_2018
FROM orders o
JOIN order_payments op
ON o.order_id = op.order_id
GROUP BY 1
ORDER BY 4 DESC;