generated from yandex-praktikum/de-project-sprint-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5_shipping_status.sql
65 lines (60 loc) · 1.56 KB
/
5_shipping_status.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
-- Creating the "shipping_status" table
drop table if exists
de.public.shipping_status cascade;
create table
de.public.shipping_status
(
shippingid bigint,
status text,
state text,
shipping_start_fact_datetime timestamp,
shipping_end_fact_datetime timestamp
);
-- Filling in the "shipping_status" table
insert into
de.public.shipping_status
(shippingid, status, state, shipping_start_fact_datetime, shipping_end_fact_datetime)
with max_dt_cte as
(
select
shippingid,
max(state_datetime::timestamp) as "state_datetime_max"
from
de.public.shipping
group by
shippingid
),
state_booked_cte as
(
select
shippingid,
state_datetime::timestamp as "shipping_start_fact_datetime"
from
de.public.shipping
where
state = 'booked'
),
state_recieved_cte as
(
select
shippingid,
state_datetime::timestamp as "shipping_end_fact_datetime"
from
de.public.shipping
where
state = 'recieved'
)
select
mdc.shippingid::bigint as "shippingid",
s.status::text as "status",
s.state::text as "state",
sbc.shipping_start_fact_datetime::timestamp as "shipping_start_fact_datetime",
src.shipping_end_fact_datetime::timestamp as "shipping_end_fact_datetime"
from
max_dt_cte as mdc
inner join de.public.shipping as s
on mdc.state_datetime_max = s.state_datetime
left join state_booked_cte as sbc
on mdc.shippingid = sbc.shippingid
left join state_recieved_cte as src
on mdc.shippingid = src.shippingid