-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData cleaning.sql
More file actions
189 lines (129 loc) · 4.28 KB
/
Data cleaning.sql
File metadata and controls
189 lines (129 loc) · 4.28 KB
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
/*
Cleaning Data in SQL querries
*/
---------------------------------------------------------------------------------------------------
-- stabndardise Date Format
select *
from cleaning
select SaleDate, convert (date,SaleDate)
from cleaning
update cleaning
set SaleDate = convert(Date,SaleDate)
-- it didnt convert so we alter the table to add a new column
Alter Table cleaning
Add SaleDateConverted Date
update cleaning
set SaleDateConverted = convert (Date,SaleDate)
---------------------------------------------------------------------------------------------------
--populate Property Address data
Select *
from cleaning
--where propertyAddress is null
order by ParcelID
Select a.ParcelID, a.PropertyAddress, b.ParcelID, b.PropertyAddress, isnull(a.propertyAddress, b.PropertyAddress)
from cleaning a
join cleaning b
on a.ParcelID = b.ParcelID
And a.[UniqueID ]<> b.[UniqueID ]
where a.propertyAddress is null
order by a.ParcelID
Update a
set propertyAddress = isnull(a.propertyAddress, b.PropertyAddress)
from cleaning a
join cleaning b
on a.ParcelID = b.ParcelID
And a.[UniqueID ]<> b.[UniqueID ]
where a.propertyAddress is null
---------------------------------------------------------------------------------------------------
--Breaking out PropertyAddress into Individual columns ( Address, City, State)
select *
from cleaning
--where propertyaddress is null
--order by ParcelID
-- starting at a very first value in propertyaddress and then look for the comma
select
substring(PropertyAddress, 1, charindex(',', PropertyAddress)-1) as Address
, substring(PropertyAddress, charindex(',', PropertyAddress)+1, Len(propertyAddress)) as City
from cleaning
Alter Table cleaning
Add PropertySplitAddress Nvarchar(255)
update cleaning
Set PropertySplitAddress = substring(PropertyAddress, 1, charindex(',', PropertyAddress)-1)
Alter Table cleaning
Add PropertySplitCity Nvarchar(255)
update cleaning
Set PropertySplitCity = substring(PropertyAddress, charindex(',', PropertyAddress)+1, Len(propertyAddress))
---------------------------------------------------------------------------------------------------
----Breaking out OwnerAddress into Individual columns ( Address, City, State)
select *
from cleaning
select
Parsename(replace(OwnerAddress,',', '.' ),3),
Parsename(replace(OwnerAddress,',','.'),2),
Parsename(replace(OwnerAddress,',','.'),1)
from cleaning
Alter Table cleaning
Add OwnerSplitAddress Nvarchar(255)
update cleaning
set OwnerSplitAddress = Parsename(replace(OwnerAddress,',', '.' ),3)
Alter Table cleaning
Add OwnerSplitCity Nvarchar(255)
update cleaning
set OwnerSplitCity = Parsename(replace(OwnerAddress,',', '.' ),2)
Alter Table cleaning
Add OwnerSplitState Nvarchar(255)
update cleaning
set OwnerSplitState = Parsename(replace(OwnerAddress,',', '.' ),1)
---------------------------------------------------------------------------------------------------
--Change Y and N to Yes and No in "sold as Vacant" field
select distinct (soldAsvacant), count(soldAsvacant)
from cleaning
group by soldasvacant
update cleaning
set SoldAsVacant =
CASE
when soldAsvacant = 'Y' then 'Yes'
when soldAsvacant = 'N' then 'No'
Else SoldAsvacant
End
---------------------------------------------------------------------------------------------------
--Remove Duplicates
with RowNumCTE as (
select *,
row_number() over (
partition by ParcelID,
PropertyAddress,
SalePrice,
SaleDate,
LegalReference
order by uniqueID ) row_num
from cleaning
)
select *
from RowNumCTE
where row_num > 1
order by PropertyAddress
-- now remove them
with RowNumCTE as (
select *,
row_number() over (
partition by ParcelID,
PropertyAddress,
SalePrice,
SaleDate,
LegalReference
order by uniqueID ) row_num
from cleaning
)
Delete
from RowNumCTE
where row_num >1
--order by PropertyAddress
---------------------------------------------------------------------------------------------------
--Delete Unused Columns
select *
from Cleaning
alter table cleaning
drop column owneraddress, taxdistrict, propertyaddress
alter table cleaning
drop column saleDate