-
Notifications
You must be signed in to change notification settings - Fork 0
/
Script-CTE.sql
198 lines (183 loc) · 3.71 KB
/
Script-CTE.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
-- staging customer
with oderdetail as (
select
s.salesorderid,
s.customerid
from
sales.salesorderheader s
),
customer as (
select
c.customerid,
c.personid
from
sales.customer c
),
person as (
select
p.businessentityid as personid,
p.firstname,
p.middlename,
p.lastname,
p.modifieddate
from
person.person p
),
detail as (
select
oderdetail.salesorderid as orderid,
person.firstname,
person.middlename,
person.lastname,
person.modifieddate
from
oderdetail
inner join customer on (customer.customerid = oderdetail.customerid)
inner join person on (person.personid = customer.personid)
)
select * from detail
-- staging shipp
with orderdetail as (
select
s.salesorderid,
s.shiptoaddressid,
s.shipdate
from
sales.salesorderheader s
),
address as (
select
a.addressid,
a.stateprovinceid,
a.addressline1,
a.modifieddate
from
person.address a
),
stateprovince as (
select
s.stateprovinceid,
s.countryregioncode,
s."name" as nmestate
from
person.stateprovince s
),
countryregion as (
select
c2.countryregioncode,
c2."name" as nmeregion
from
person.countryregion c2
),
detail as (
select
orderdetail.salesorderid as orderid,
address.addressline1,
nmestate,
nmeregion,
orderdetail.shipdate,
address.modifieddate
from
orderdetail
inner join address on (address.addressid = orderdetail.shiptoaddressid)
inner join stateprovince on (stateprovince.stateprovinceid = address.stateprovinceid)
inner join countryregion on (countryregion.countryregioncode = stateprovince.countryregioncode)
)
select * from detail
-- staging product
with orderdetail as (
select
s.salesorderid as orderid,
s.productid,
s.unitprice,
s.orderqty
from
sales.salesorderdetail s
),
product as (
select
productid,
name as nmeproduct,
productnumber,
modifieddate
from
production.product
),
detail as (
select
orderid,
nmeproduct,
productnumber,
orderqty,
unitprice,
modifieddate
from
orderdetail
inner join product on (product.productid = orderdetail.productid)
)
select * from detail
-- stating creditcard
with orderdetail as (
select
s.salesorderid as orderid,
s.creditcardid
from
sales.salesorderheader s
),
creditcard as (
select
creditcardid,
cardtype,
cardnumber,
modifieddate
from {{ source('indiciumdesafiofinal', 'creditcard') }}
),
detail as (
select
row_number() over (order by creditcardid) as sk_creditcardid,
orderid,
cardtype,
cardnumber,
modifieddate
from
orderdetail
inner join creditcard on (creditcard.creditcardid = orderdetail.creditcardid)
)
select * from detail
-- staging orderdetail
with salesorder as (
select
s.salesorderid as orderid,
s.orderdate,
s.status,
s.modifieddate
from
sales.salesorderheader s
),
salesreason as (
select
s2.salesorderid,
s2.salesreasonid
from
sales.salesorderheadersalesreason s2
),
reason as (
select
s.salesreasonid,
s.name as nmereason
from
sales.salesreason s
),
detail as (
select
orderid,
orderdate,
status,
nmereason,
modifieddate
from
salesorder
inner join salesreason on (salesreason.salesorderid = salesorder.orderid)
inner join reason on (reason.salesreasonid = salesreason.salesreasonid)
)
select * from detail