-
Notifications
You must be signed in to change notification settings - Fork 0
/
orders_original.sql
58 lines (33 loc) · 1.12 KB
/
orders_original.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
with orders as (
select * from "postgres"."staging"."stg_orders"
),
payments as (
select * from "postgres"."staging"."stg_payments"
),
order_payments as (
select
order_id,
sum(case when payment_method = 'credit_card' then amount else 0 end) as credit_card_amount,
sum(case when payment_method = 'coupon' then amount else 0 end) as coupon_amount,
sum(case when payment_method = 'bank_transfer' then amount else 0 end) as bank_transfer_amount,
sum(case when payment_method = 'gift_card' then amount else 0 end) as gift_card_amount,
sum(amount) as total_amount
from payments
group by order_id
),
final as (
select
orders.order_id,
orders.customer_id,
orders.order_date,
orders.status,
order_payments.credit_card_amount,
order_payments.coupon_amount,
order_payments.bank_transfer_amount,
order_payments.gift_card_amount,
order_payments.total_amount as amount
from orders
left join order_payments
on orders.order_id = order_payments.order_id
)
select * from final