-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData Cleaning Portfolio Project Queries.sql
More file actions
169 lines (113 loc) · 4.41 KB
/
Data Cleaning Portfolio Project Queries.sql
File metadata and controls
169 lines (113 loc) · 4.41 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
/*
Cleaning Data in SQL Queries
*/
Select *
From NashvilleHousing
--------------------------------------------------------------------------------------------------------------------------
-- Standardize Date Format
-- Formatted the sale date to YYYY-MM-DD
SELECT saledate, convert(date,saledate)
FROM nashvillehousing
Update nashvillehousing
SET Saledate = convert(date,saledate)
ALTER TABLE nashvillehousing
ADD SaleDateConverted Date;
Update nashvillehousing
SET SaleDateConverted = convert(date,saledate);
--------------------------------------------------------------------------------------------------------------------------
-- Populate Property Address data
-- There are NULL values in the dataset
-- ParcelID matches with an address
--SELECT *
--FROM nashvillehousing
--where propertyaddress IS NULL
-- join table with itself
SELECT a.parcelID, a.propertyaddress, b.parcelID, b.propertyaddress, ISNULL(a.propertyaddress, b.propertyaddress)
FROM nashvillehousing a
JOIN 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 nashvillehousing a
JOIN nashvillehousing b ON a.parcelID = b.parcelID
AND a.[uniqueID] <> b.[uniqueID]
WHERE a.propertyaddress is NULL
SELECT *
FROM nashvillehousing
where propertyaddress IS NULL
--------------------------------------------------------------------------------------------------------------------------
-- Breaking out Address into Individual Columns (Address, City, State)
-- Using substring, character index
SELECT
SUBSTRING(propertyaddress, 1, Charindex(',', propertyaddress)-1) as Address
, SUBSTRING(propertyaddress, Charindex(',', propertyaddress)+1, LEN(propertyaddress)) as Address
FROM nashvillehousing
ALTER TABLE nashvillehousing
ADD PropertySplitAddress VARCHAR(250);
Update nashvillehousing
SET PropertySplitAddress = SUBSTRING(propertyaddress, 1, Charindex(',', propertyaddress)-1)
ALTER TABLE nashvillehousing
ADD PropertySplitCity VARCHAR(250);
Update nashvillehousing
SET PropertySplitCity = SUBSTRING(propertyaddress, Charindex(',', propertyaddress)+1, LEN(propertyaddress));
-- Breaking out OwnerAddress into Individual Columns (Address, City, State) Using PARSENAME
SELECT
PARSENAME(REPLACE(OwnerAddress, ',', '.'), 3),
PARSENAME(REPLACE(OwnerAddress, ',', '.'), 2),
PARSENAME(REPLACE(OwnerAddress, ',', '.'), 1)
FROM nashvillehousing
ALTER TABLE nashvillehousing
ADD OwnerSplitAddress NVARCHAR(250)
Update nashvillehousing
SET OwnerSplitAddress = PARSENAME(REPLACE(OwnerAddress, ',', '.'), 3)
ALTER TABLE nashvillehousing
ADD OwnerSplitCity NVARCHAR(250)
Update nashvillehousing
SET OwnerSplitCity = PARSENAME(REPLACE(OwnerAddress, ',', '.'), 2)
ALTER TABLE nashvillehousing
ADD OwnerSplitState NVARCHAR(250)
Update nashvillehousing
SET OwnerSplitState = PARSENAME(REPLACE(OwnerAddress, ',', '.'), 1);
SELECT *
FROM nashvillehousing;
--------------------------------------------------------------------------------------------------------------------------
-- Change Y and N to Yes and No in "Sold as Vacant" field
SELECT DISTINCT(SoldAsVacant), Count(Soldasvacant)
FROM nashvillehousing
GROUP BY SoldAsVacant
order by 2
SELECT SoldAsVacant,
CASE WHEN SoldAsVacant = 'Y' THEN 'Yes'
WHEN SoldAsVacant ='N' THEN 'No'
Else SoldAsVacant
END
FROM nashvillehousing
UPDATE nashvillehousing
SET SoldAsVacant = CASE WHEN SoldAsVacant = 'Y' THEN 'Yes'
WHEN SoldAsVacant ='N' THEN 'No'
Else SoldAsVacant
END
-----------------------------------------------------------------------------------------------------------------------------------------------------------
-- Remove Duplicates
-- CTE
WITH RowNumCTE AS(
SELECT*,
ROW_number() OVER (
PARTITION BY ParcelID, PropertyAddress, SalePrice, SaleDate, LegalReference
ORDER BY UniqueID
) row_num
FROM nashvillehousing
)
DELETE
FROM RowNumCTE
WHERE row_num > 1
---------------------------------------------------------------------------------------------------------
-- Delete Unused Columns
-- Deleting original owner address and property adddress after spliting them
SELECT*
FROM nashvillehousing
ALTER TABLE nashvillehousing
DROP COLUMN OwnerAddress, TaxDistrict, PropertyAddress
ALTER TABLE nashvillehousing
DROP COLUMN SaleDate