This repository was archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_merge_two.py
214 lines (197 loc) · 5.94 KB
/
db_merge_two.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import pyodbc
from dataclasses import dataclass
import datetime
import re
import sys
@dataclass
class factTableRow:
Contract_ContractId: int
Contract_CustomerId: int
Contract_ServiceId: int
Contract_DeviceId: int
Contract_StoreId: int
Contract_LocationId: int
Contract_ContractSignDate: datetime.date
Contract_ContractValidFrom: datetime.date
Contract_ContractValidTo: datetime.date
Contract_ContractName: str
Contract_ContractType: str
Contract_ContractState: str
Contract_ContractPrice: float
Customer_CustomerId: int
Customer_FirstName: str
Customer_LastName: str
Customer_Phone: str
Customer_BirthDate: datetime.date
Customer_Gender: str
Device_DeviceId: int
Device_Name: str
Device_Brand: str
Device_Type: str
Device_Price: float
Store_StoreId: int
Store_Name: str
Store_Phone: str
Location_LocationId: int
Location_Street: str
Location_City: str
Location_Region: str
Location_ZipCode: str
Location_Country: str
server = sys.argv[1]
database = 'TIAE6_DT_Staging'
username = 'staging'
password = 'python'
cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
cursor = cnxn.cursor()
cursor.execute("""
SELECT
--factTable
Contract.ContractId,
Contract.CustomerId,
Contract.ServiceId,
Contract.DeviceId,
Contract.StoreId,
Store.LocationId,
Contract.ContractSignDate,
Contract.ContractValidFrom,
Contract.ContractValidTo,
Service.Name,
Service.Type,
Service.State,
Service.Price,
--DimCustomer
Customer.CustomerId,
Customer.FirstName,
Customer.LastName,
Customer.Phone,
Customer.BirthDate,
Customer.Gender,
--DimDevice
Device.DeviceId,
Device.Name,
Device.Brand,
Device.Type,
Device.Price,
--DimStore
Store.StoreId,
Store.Name,
Store.Phone,
--DimLocation
Location.LocationId,
Location.Street,
Location.City,
Location.Region,
Location.ZipCode,
Location.Country
FROM TIAE6_DT_Staging.dbo.Contract
JOIN TIAE6_DT_Staging.dbo.Customer ON Customer.CustomerId = Contract.CustomerId
JOIN TIAE6_DT_Staging.dbo.Service ON Service.ServiceId = Contract.ServiceId
JOIN TIAE6_DT_Staging.dbo.Device ON Device.DeviceId = Contract.DeviceId
JOIN TIAE6_DT_Staging.dbo.Store ON Store.StoreId = Contract.StoreId
JOIN TIAE6_DT_Staging.dbo.Location ON Location.LocationId = Store.LocationId
""")
factTable = []
def get_or_create_date_row_id(table, value):
tablename = "Dim" + table
id = table + "Id"
year = value.year
month = value.month
week = value.strftime("%W")
day = value.day
cursor.execute("SELECT * FROM {} WHERE Year = {} AND Month = {} AND Week = {} AND Day = {}".format(tablename, year, month, week, day))
row = cursor.fetchone()
if row:
return row[0]
cursor.execute("INSERT INTO {} (Year, Month, Week, Day) VALUES ({},{},{},{})".format(tablename, year, month, week, day))
cnxn.commit()
cursor.execute("SELECT * FROM {} WHERE Year = {} AND Month = {} AND Week = {} AND Day = {}".format(tablename, year, month, week, day))
row = cursor.fetchone()
if row:
return row[0]
for row in cursor.fetchall():
dr = factTableRow(*list(row))
# Die Spalte «Phone» darf nur nummerische Zeichen beinhalten.
dr.Customer_Phone = re.sub('[^0-9]+', '', dr.Customer_Phone)
dr.Store_Phone = re.sub('[^0-9]+', '', dr.Store_Phone)
ContractSignDateId = get_or_create_date_row_id('ContractSignDate', dr.Contract_ContractSignDate)
ContractValidFromId = get_or_create_date_row_id('ContractValidFrom', dr.Contract_ContractValidFrom)
ContractValidToId = get_or_create_date_row_id('ContractValidTo', dr.Contract_ContractValidTo)
# Insert dimensions
try:
cursor.execute("INSERT INTO DimCustomer (CustomerId, FirstName, LastName, Phone, BirthDate, Gender) VALUES ({},'{}','{}','{}','{}','{}')".format(
dr.Customer_CustomerId,
dr.Customer_FirstName,
dr.Customer_LastName,
dr.Customer_Phone,
dr.Customer_BirthDate,
dr.Customer_Gender
))
cnxn.commit()
except:
# If error then probably it is a duplication error. Just ignore it
pass
try:
cursor.execute("INSERT INTO DimLocation (LocationId, Street, City, Region, ZipCode, Country) VALUES ({},'{}','{}','{}','{}','{}')".format(
dr.Contract_LocationId,
dr.Location_Street,
dr.Location_City,
dr.Location_Region,
dr.Location_ZipCode,
dr.Location_Country
))
cnxn.commit()
except:
pass
try:
cursor.execute("INSERT INTO DimStore (StoreId, StoreName, StorePhone) VALUES ({},'{}','{}')".format(
dr.Store_StoreId,
dr.Store_Name,
dr.Store_Phone
))
cnxn.commit()
except:
# If error then probably it is a duplication error. Just ignore it
pass
try:
cursor.execute("INSERT INTO DimDevice (DeviceId, DeviceName, DeviceBrand, DeviceType, DevicePrice, PurchasePrice, CompanyName, Phone, IsPrivateLabel, DeviceWareHouseAmount, WareHouseName, WareHousePhone, WareHouseCapacity) VALUES ({},'{}','{}','{}',{},{},'{}','{}',{},{},'{}','{}',{})".format(
dr.Device_DeviceId,
dr.Device_Name,
dr.Device_Brand,
dr.Device_Type,
dr.Device_Price,
0.00,
'ErrorParsing',
'ErrorParsing',
0,
0.00,
'ErrorParsing',
'ErrorParsing',
0
))
cnxn.commit()
except:
# If error then probably it is a duplication error. Just ignore it
pass
# Insert fact table
try:
cursor.execute("INSERT INTO FactSales (ContractId, CustomerId, DeviceId, StoreId, LocationId, ContractSignDateId, ContractValidFromId, ContractValidToId, ContractName, ContractType, ContractState, ContractPrice) VALUES ({},{},{},{},{},{},{},{},'{}','{}','{}',{})".format(
dr.Contract_ContractId,
dr.Contract_CustomerId,
dr.Contract_DeviceId,
dr.Contract_StoreId,
dr.Contract_LocationId,
ContractSignDateId,
ContractValidFromId,
ContractValidToId,
dr.Contract_ContractName,
dr.Contract_ContractType,
dr.Contract_ContractState,
dr.Contract_ContractPrice
))
cnxn.commit()
except:
# If error then probably it is a duplication error. Just ignore it
pass
print("Merging Staging.Production to Staging.DWH complete.")
print("Transformation of 1st Class have been applied successfully.")