-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFifaDataQuery.sql
242 lines (182 loc) · 8.6 KB
/
FifaDataQuery.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
-- Create a temporary column that stores the first substring from the playerUrl
alter table FifaData.dbo.FIFA2021DATA
add TempCol Nvarchar(255);
-- Reversing the string and counting manually counting from the number of strings before the name
-- Which is where the substring will start extracting from
-- Updating the created column with the substring data
update FifaData.dbo.FIFA2021DATA
set TempCol = substring(reverse(playerUrl), 9, LEN(playerUrl))
from FifaData.dbo.FIFA2021DATA
-- Create the fullname column that stores the second substring from the TempCol
-- Which is the names of the players
alter table FifaData.dbo.FIFA2021DATA
add Fullname Nvarchar(255);
-- Update the fullname column with string extracted from the TempCol
update FifaData.dbo.FIFA2021DATA
set Fullname = reverse(substring((TempCol), 1, CHARINDEX('/', TempCol)-1))
from FifaData.dbo.FIFA2021DATA
-- Droppping uneccessary columns
alter table FifaData.dbo.FIFA2021DATA
drop column TempCol, TempCol2, Name, LongName, photoUrl, playerUrl
-- Replace the '-' with space and capitalize the names
update FifaData.dbo.FIFA2021DATA
set Fullname = upper(replace(Fullname, '-', ' '))
from FifaData.dbo.FIFA2021DATA
alter table FifaData.dbo.FIFA2021DATA
add POT_ DECIMAL(5, 2), BOV_ DECIMAL(5, 2);
alter table FifaData.dbo.FIFA2021DATA
drop column [↓OVA];
update FifaData.dbo.FIFA2021DATA
set BOV_ = BOV/100.0
from FifaData.dbo.FIFA2021DATA
select * from FifaData.dbo.FIFA2021DATA
where OVA = 0
SELECT Height,
CASE
-- If the value contains 'cm', convert cm to inches (1 cm = 0.393701 inches)
WHEN Height LIKE '%cm' THEN CAST(REPLACE(Height, 'cm', '') AS FLOAT) * 0.393701
-- If the value contains feet and inches (e.g., 6'7"), convert to inches (1 foot = 12 inches)
WHEN Height LIKE '%"%' THEN
CAST(LEFT(Height, 1) AS FLOAT) * 12 +
CAST(SUBSTRING(Height, len(Height) - 1, 1) AS FLOAT)
-- For other cases, assume the value is already in inches
ELSE CAST(Height AS FLOAT)
END AS HeightInInches
FROM FifaData.dbo.FIFA2021DATA
-- Create new column namely Height(Inch)
alter table FifaData.dbo.FIFA2021DATA
add Height_In_Inch FLOAT;
-- Convert Height to inches and update the table
update FifaData.dbo.FIFA2021DATA
set Height_In_Inch = CASE
-- If the value contains 'cm', convert cm to inches (1 cm = 0.393701 inches)
WHEN Height LIKE '%cm' THEN CAST(REPLACE(Height, 'cm', '') AS FLOAT) * 0.393701
-- If the value contains feet and inches (e.g., 6'7"), convert to inches (1 foot = 12 inches)
WHEN Height LIKE '%"%' THEN CAST(LEFT(Height, 1) AS FLOAT) * 12 + CAST(SUBSTRING(Height, len(Height) - 1, 1) AS FLOAT)
-- For other cases, assume the value is already in inches
ELSE CAST(Height AS FLOAT)
END
from FifaData.dbo.FIFA2021DATA
-- Create new column namely Weight(lbs)
alter table FifaData.dbo.FIFA2021DATA
add Weight_In_Lbs FLOAT;
-- Convert Weight to Lbs and update the table
update FifaData.dbo.FIFA2021DATA
set Weight_In_Lbs = CASE
-- If the value contains 'kg', convert kg to lbs (1 kg = 2.20462 pounds)
WHEN Weight LIKE '%kg' THEN CAST(REPLACE(Weight, 'kg', '') AS FLOAT) * 2.20462
-- If the value contains lbs, remove the appended 'lbs' and convert to float
WHEN Weight LIKE '%lbs' THEN CAST(REPLACE(Weight, 'lbs', '') AS FLOAT)
-- For other cases, assume the value is already in lbs
ELSE CAST(Weight AS FLOAT)
END
from FifaData.dbo.FIFA2021DATA
-- Drop the initial weight and height columns
alter table FifaData.dbo.FIFA2021DATA
drop column POT, BOV
-- Working on the value, Wage, Release Clause Columns
-- Adding new columns to represent them
alter table FifaData.dbo.FIFA2021DATA
add ValueIN$ float, WageIN$ float, Release_ClauseIN$ float
update FifaData.dbo.FIFA2021DATA
set ValueIN$ =
case
-- Eliminate "€", "M", convert to million then convert to $ using $1.183 per euro xchnage rate
when Value like '%€%' and Value like '%M%' then (cast(replace(replace(Value, '€', ''), 'M', '') as float) * 1000000) * 1.183
-- Eliminate "€", "K", convert to thousand then convert to $ using $1.183 per euro xchnage rate
when Value like '%€%' and Value like '%K%' then (cast(replace(replace(Value, '€', ''), 'K', '') as float) * 1000) * 1.183
-- Eliminate "€", convert to float then convert to $ using $1.183 per euro xchnage rate
else cast(replace(Value, '€', '') as float) * 1.183
end,
WageIN$ =
case
-- Eliminate "€", "M", convert to million then convert to $ using $1.183 per euro xchnage rate
when Wage like '%€%' and Wage like '%M%' then (cast(replace(replace(Wage, '€', ''), 'M', '') as float) * 1000000) * 1.183
-- Eliminate "€", "K", convert to thousand then convert to $ using $1.183 per euro xchnage rate
when Wage like '%€%' and Wage like '%K%' then (cast(replace(replace(Wage, '€', ''), 'K', '') as float) * 1000) * 1.183
-- Eliminate "€", convert to float then convert to $ using $1.183 per euro xchnage rate
else cast(replace(Wage, '€', '') as float) * 1.183
end,
Release_ClauseIN$ =
case
-- Eliminate "€", "M", convert to million then convert to $ using $1.183 per euro xchnage rate
when [Release Clause] like '%€%' and [Release Clause] like '%M%' then (cast(replace(replace([Release Clause], '€', ''), 'M', '') as float) * 1000000) * 1.183
-- Eliminate "€", "K", convert to thousand then convert to $ using $1.183 per euro xchnage rate
when [Release Clause] like '%€%' and [Release Clause] like '%K%' then (cast(replace(replace([Release Clause], '€', ''), 'K', '') as float) * 1000) * 1.183
-- Eliminate "€", convert to float then convert to $ using $1.183 per euro xchnage rate
else cast(replace([Release Clause], '€', '') as float) * 1.183
end
from FifaData.dbo.FIFA2021DATA
-- Creating the Contract status column
alter table FifaData.dbo.FIFA2021DATA
add ContractStatus nvarchar(255)
-- Updating the contract status column with the Contract column
update FifaData.dbo.FIFA2021DATA
set ContractStatus =
case
when Contract like '%Free%' then Contract
when Contract like '%Loan%' then 'On Loan'
else 'On Contract'
end
from FifaData.dbo.FIFA2021DATA
--Creating the ContractStartDate column and the ContractEndDate column
alter table FifaData.dbo.FIFA2021DATA
add ContractStartDate date, ContractEndDate nvarchar(255)
-- Updating the ContractStartDate column using the Joined column
update FifaData.dbo.FIFA2021DATA
set ContractStartDate =
case
when Contract not like '%Loan%' and Contract not like '%Free%' and isdate(Joined) = 1 then Joined
else null
end
from FifaData.dbo.FIFA2021DATA
---- Updating the ContractEndDate column using the Joined column
update FifaData.dbo.FIFA2021DATA
set ContractEndDate =
case
when Contract not like '%Loan%' and Contract not like '%Free%'
then parsename(replace(Contract, '~', '.'), 1)
else null
end
from FifaData.dbo.FIFA2021DATA
--Creating the LoanStartDate column and the LoanEndDate column
alter table FifaData.dbo.FIFA2021DATA
add LoanStartDate date, LoanEndDate date
-- Updating the LoanStartDate
update FifaData.dbo.FIFA2021DATA
set LoanStartDate =
case
when [Loan Date End] is not null and isdate(Joined) = 1 then Joined
else null
end
from FifaData.dbo.FIFA2021DATA
-- Updating the LoanEndDate
update FifaData.dbo.FIFA2021DATA
set LoanEndDate =
case
when [Loan Date End] is not null and isdate([Loan Date End]) = 1 then [Loan Date End]
else null
end
from FifaData.dbo.FIFA2021DATA
--Dropping columns
alter table FifaData.dbo.FIFA2021DATA
drop column Contract, Joined, [Loan Date End]
-- Creating new column for Club
ALTER TABLE FifaData.dbo.FIFA2021DATA
ADD Club_ NVARCHAR(255) COLLATE SQL_Latin1_General_CP1_CI_AI;
-- Update the new club column with carefully formatted column
update FifaData.dbo.FIFA2021DATA
set Club_ = rtrim(ltrim(Club))
from FifaData.dbo.FIFA2021DATA
-- Creating new columns for W/F, SM, IR
ALTER TABLE FifaData.dbo.FIFA2021DATA
ADD [W/F_] int, SM_ int, IR_ int
-- Updating the newly created columns
update FifaData.dbo.FIFA2021DATA
set [W/F_] = rtrim(ltrim(replace([W/F], '★', ''))),
SM_ = rtrim(ltrim(replace(SM, '★', ''))),
IR_ = rtrim(ltrim(replace(IR, '★', '')))
from FifaData.dbo.FIFA2021DATA
--Dropping the intial columns
alter table FifaData.dbo.FIFA2021DATA
drop column [W/F], SM, IR