-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data Cleaning PortfolioProject.sql
201 lines (150 loc) · 5.82 KB
/
Data Cleaning PortfolioProject.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
-- Data Cleaning Portfolio Project: Nashville Housing Data
-- This project focuses on cleaning and standardizing a real-world dataset on Nashville housing transactions.
--View our data:
SELECT *
FROM PortfolioProject..Nashville_Housing
--PART 1
--Standardize the Date
--Create a column called SaleDateConverted where we will add our standardized date
ALTER TABLE Nashville_Housing
ADD SaleDateConverted Date
--Populate the new column with our standardized data
UPDATE Nashville_Housing
SET SaleDateConverted = CONVERT(DATE, SaleDate)
--View the results
SELECT SaleDateConverted, SaleDate
FROM PortfolioProject..Nashville_Housing
--PART 2
--Populate Property Address Data
--View the "PropertyAddress" column
SELECT PropertyAddress
FROM PortfolioProject..Nashville_Housing
--WHERE PropertyAddress IS NULL
ORDER BY ParcelID
--The null "PropertyAddress" values are supposed have the same "ParcelID" as some other non-Null values
--We will use a self-join to see this and populate the NULL values later
SELECT a.ParcelID, a.PropertyAddress, b.ParcelID, b.PropertyAddress, ISNULL(a.PropertyAddress, b.PropertyAddress)
FROM PortfolioProject..Nashville_Housing a
JOIN PortfolioProject..Nashville_Housing b
ON a.ParcelID = b.ParcelID
AND a.[UniqueID ] <> b.[UniqueID ]
WHERE a.PropertyAddress IS NULL
--Populate the NULL values
UPDATE a
SET PropertyAddress = ISNULL(a.PropertyAddress, b.PropertyAddress)
FROM PortfolioProject..Nashville_Housing a
JOIN PortfolioProject..Nashville_Housing b
ON a.ParcelID = b.ParcelID
AND a.[UniqueID ] <> b.[UniqueID ]
--PART 3
--Breaking Out Address Into Individual Columns (Address, City, State)
--View the data
SELECT PropertyAddress
FROM PortfolioProject..Nashville_Housing
--WHERE PropertyAddress IS NULL
--ORDER BY ParcelID
--Use "SUBSTRING" to split the "PropertyAddress" and view the results.
--NOTE: No changes are made to the data. This is only for viewing. The changes are made in the next code.
SELECT PropertyAddress,
SUBSTRING(PropertyAddress, 1, CHARINDEX(',', PropertyAddress) - 1) AS Address,
SUBSTRING(PropertyAddress, CHARINDEX(',', PropertyAddress) + 1, LEN(PropertyAddress)) AS Address_2
FROM PortfolioProject..Nashville_Housing
--Create new column "PropertySplitAddress" and populate with the split data
ALTER TABLE Nashville_Housing
ADD PropertySplitAddress nvarchar(255)
UPDATE Nashville_Housing
SET PropertySplitAddress = SUBSTRING(PropertyAddress, 1, CHARINDEX(',', PropertyAddress) - 1)
--Create new column "PropertySplitCity" and populate with the split data
ALTER TABLE Nashville_Housing
ADD PropertySplitCity nvarchar(255)
UPDATE Nashville_Housing
SET PropertySplitCity = SUBSTRING(PropertyAddress, CHARINDEX(',', PropertyAddress) + 1, LEN(PropertyAddress))
--View the results
SELECT *
FROM PortfolioProject..Nashville_Housing
--Next, we are going to do the same for the "OwnerAddress" column.
SELECT OwnerAddress
FROM PortfolioProject..Nashville_Housing
--Use "PARSENAME" to split the "OwnerAddress" and view the results.
--NOTE: No changes are made to the data. This is only for viewing. The changes are made in the next code.
SELECT OwnerAddress,
PARSENAME(REPLACE(OwnerAddress, ',', '.'), 3) AS OwnerSplitAddress,
PARSENAME(REPLACE(OwnerAddress, ',', '.'), 2) AS OwnerSplitCity,
PARSENAME(REPLACE(OwnerAddress, ',', '.'), 1) AS OwnerSplitState
FROM PortfolioProject..Nashville_Housing
--Create Owner Address Column and populate it with the results:
ALTER TABLE Nashville_Housing
ADD OwnerSplitAddress nvarchar(255)
UPDATE Nashville_Housing
SET OwnerSplitAddress = PARSENAME(REPLACE(OwnerAddress, ',', '.'), 3)
--Create Owner City Column and populate it with the results:
ALTER TABLE Nashville_Housing
ADD OwnerSplitCity nvarchar(255)
UPDATE Nashville_Housing
SET OwnerSplitCity = PARSENAME(REPLACE(OwnerAddress, ',', '.'), 2)
--Create Owner State Column and populate it with the results:
ALTER TABLE Nashville_Housing
ADD OwnerSplitState nvarchar(255)
UPDATE Nashville_Housing
SET OwnerSplitState = PARSENAME(REPLACE(OwnerAddress, ',', '.'), 1)
--View the results
SELECT *
FROM PortfolioProject..Nashville_Housing
--PART 4
--Change Y and N to Yes and No in "Sold as Vacant" field:
--View the number of different Yes and No in the column
--We can see that there is both "Y" and "Yes". There is also both "N" and "No". This is what we must change.
SELECT DISTINCT(SoldAsVacant), COUNT(SoldAsVacant)
FROM PortfolioProject..Nashville_Housing
GROUP BY SoldAsVacant
ORDER BY 2
--Use "CASE" to see how the changes would occur
SELECT SoldAsVacant,
CASE
WHEN SoldAsVacant = 'Y' THEN 'Yes'
WHEN SoldAsVacant = 'N' THEN 'No'
ELSE SoldAsVacant
END
FROM PortfolioProject..Nashville_Housing
--Apply the changes
UPDATE Nashville_Housing
SET SoldAsVacant =
CASE
WHEN SoldAsVacant = 'Y' THEN 'Yes'
WHEN SoldAsVacant = 'N' THEN 'No'
ELSE SoldAsVacant
END
--PART 5:
--Remove duplicates
--We will use "ROW_NUMBER" to do this
WITH RowNumCTE AS (
SELECT *,
ROW_NUMBER() OVER(
PARTITION BY ParcelID,
PropertyAddress,
SalePrice,
SaleDate,
LegalReference
ORDER BY
UniqueID
) row_num
FROM PortfolioProject..Nashville_Housing
)
DELETE
FROM RowNumCTE
WHERE row_num > 1
--ORDER BY PropertyAddress
--View the results
SELECT *
FROM PortfolioProject..Nashville_Housing
--PART 6
--Delete Unused Columns:
--View the data
SELECT *
FROM PortfolioProject..Nashville_Housing
--Drop OwnerAddress, TaxDistrict, PropertyAddress because we already created columns with their split data
ALTER TABLE PortfolioProject..Nashville_Housing
DROP COLUMN OwnerAddress, TaxDistrict, PropertyAddress
--We also dropped SaleDate because we created a standardized date column for it earlier
ALTER TABLE PortfolioProject..Nashville_Housing
DROP COLUMN SaleDate