-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nashville Housing Data Cleaning.sql
205 lines (140 loc) · 5.57 KB
/
Nashville Housing Data Cleaning.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
/*
Nashville Housing Data for Data Cleaning
Cleaning data in SQL
*/
Select * From [PortfolioProject]..NashvilleHousing
-- Standardize Data Format --------------------------------
Select SaleDateUpdated, convert(Date,SaleDate)
From [PortfolioProject]..NashvilleHousing
Update PortfolioProject..NashvilleHousing
Set SaleDate = CONVERT(Date,SaleDate)
-- or
Alter Table NashvilleHousing
Add SaleDateUpdated Date;
Update PortfolioProject..NashvilleHousing
Set SaleDateUpdated = CONVERT(Date,SaleDate)
-------------------------------------------------
-- Populate Property Address Data
Select *
From PortfolioProject..NashvilleHousing
--Where PropertyAddress is NULL
Order BY ParcelID
Select a.ParcelID, a.PropertyAddress, b.ParcelID, b.PropertyAddress, ISNULL( a.PropertyAddress, b.PropertyAddress)
From PortfolioProject..NashvilleHousing a
Join PortfolioProject..NashvilleHousing b
On a.ParcelID = b.ParcelID
AND a.[UniqueID ] <> b.[UniqueID ]
Where a.PropertyAddress is NULL
Update a
Set PropertyAddress = ISNULL( a.PropertyAddress, b.PropertyAddress)
From PortfolioProject..NashvilleHousing a
Join PortfolioProject..NashvilleHousing b
On a.ParcelID = b.ParcelID
AND a.[UniqueID ] <> b.[UniqueID ]
Where a.PropertyAddress is NULL
------------------------------------------------------------------
-- Breaking address into Individual columns
Select PropertyAddress
From PortfolioProject..NashvilleHousing
Select
Substring(PropertyAddress,1, CHARINDEX(',',PropertyAddress)-1) As Address,
Substring(PropertyAddress,CHARINDEX(',',PropertyAddress)+1,LEN(PropertyAddress)) As City
From PortfolioProject..NashvilleHousing
Alter Table NashvilleHousing
Add Property_Address NVarchar(300);
Update PortfolioProject..NashvilleHousing
Set Property_Address = Substring(PropertyAddress,1, CHARINDEX(',',PropertyAddress)-1)
Alter Table NashvilleHousing
Add Property_City NVarchar(200);
Update PortfolioProject..NashvilleHousing
Set Property_City =Substring(PropertyAddress,CHARINDEX(',',PropertyAddress)+1,LEN(PropertyAddress))
Select PropertyAddress, Property_Address, Property_City
From PortfolioProject..NashvilleHousing
-- Owner Address
Select OwnerAddress,
PARSENAME(Replace(OwnerAddress,',','.'),3) As Owner_Address,
PARSENAME(Replace(OwnerAddress,',','.'),2) As Owner_City,
PARSENAME(Replace(OwnerAddress,',','.'),1) As Owner_State
From PortfolioProject.dbo.NashvilleHousing
Alter Table PortfolioProject..NashvilleHousing
Add Owner_Address NVarchar(300);
Update PortfolioProject..NashvilleHousing
Set Owner_Address = PARSENAME(Replace(OwnerAddress,',','.'),3)
Alter Table PortfolioProject..NashvilleHousing
Add Owner_City NVarchar(300);
Update PortfolioProject..NashvilleHousing
Set Owner_City =PARSENAME(Replace(OwnerAddress,',','.'),2)
Alter Table PortfolioProject..NashvilleHousing
Add Owner_State NVarchar(300);
Update PortfolioProject..NashvilleHousing
Set Owner_State =PARSENAME(Replace(OwnerAddress,',','.'),1)
Select * From PortfolioProject..NashvilleHousing
--------------------------------------------------------------
-- Change Y and N to Yes and No in 'Sold as Vacant' field
Select Distinct(SoldAsVacant),count(SoldAsVacant)
From PortfolioProject..NashvilleHousing
Group By SoldAsVacant
Select SoldAsVacant,
Case
When SoldAsVacant = 'Y' then 'Yes'
When SoldAsVacant = 'N' then 'No'
Else SoldAsVacant
End
From PortfolioProject..NashvilleHousing
Update PortfolioProject..NashvilleHousing
Set SoldAsVacant = Case
When SoldAsVacant = 'Y' then 'Yes'
When SoldAsVacant = 'N' then 'No'
Else SoldAsVacant
End
---------------------------------------------------------------
-- Remove Duplicates
With NashHouse As(
Select *,
ROW_NUMBER() over (
Partition By ParcelID,
PropertyAddress,
SaleDate, LegalReference
Order BY UniqueID
) row_num
From PortfolioProject..NashvilleHousing
)
Delete *
From NashHouse
Where row_num>1
Order By PropertyAddress
Select * From PortfolioProject..NashvilleHousing
----------------------------------------------------------
-- Delete Unused/Updated Columns
Select * From PortfolioProject..NashvilleHousing
Alter Table PortfolioProject..NashvilleHousing
Drop column SaleDate, PropertyAddress, OwnerAddress
------------------------------------------------------------------
Select Bedrooms, COALESCE(Bedrooms, 0)
From PortfolioProject..NashvilleHousing
---------------------------------------------------------------------------
-- Calculating Price Per Acre
Select SalePrice, Acreage, Round(SalePrice/NULLIF(Acreage,0),3) As PricePerAcre
From PortfolioProject..NashvilleHousing
Alter Table PortfolioProject..NashvilleHousing
Add PricePerAcre float;
Update PortfolioProject..NashvilleHousing
Set PricePerAcre = Round(SalePrice/NULLIF(Acreage,0),3)
------------------------------------------------------------------
Select SalePrice
From PortfolioProject..NashvilleHousing
Where SalePrice > 1000000;
--------------------------------------------
-- Impute missing BuildingValue based on LandUse
With AvgBuildingValue As(
Select LandUse, Avg(BuildingValue) As AvgBuilding
From PortfolioProject..NashvilleHousing
Group BY LandUse, BuildingValue
)
Update PortfolioProject..NashvilleHousing
Set BuildingValue = AvgBuilding
From AvgBuildingValue
Where PortfolioProject..NashvilleHousing.LandUse = AvgBuildingValue.LandUse
And PortfolioProject..NashvilleHousing.BuildingValue IS NULL;
-------------------------------------------------------------------------------------
Select * From PortfolioProject..NashvilleHousing