-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNashvilleDataCleaning.sql
More file actions
183 lines (125 loc) · 4.6 KB
/
NashvilleDataCleaning.sql
File metadata and controls
183 lines (125 loc) · 4.6 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
/*
Cleaning Data in SQL Queries
*/
select *
from PortfolioProject.dbo.NashvilleHousing;
---------------------------------------------
-- Standardize Date Format
select SaleDateConverted
from PortfolioProject.dbo.NashvilleHousing;
-- this didn't workout
update NashvilleHousing
SET SaleDate = Convert(Date, SaleDate);
-- New Command
-- creating the column
alter table NashvilleHousing
Add SaleDateConverted Date;
-- updating the column with values
update NashvilleHousing
set SaleDateConverted = convert(Date, SaleDate);
-- Populate Property Address Data
select *
from PortfolioProject.dbo.NashvilleHousing
-- where PropertyAddress is Null
order by ParcelID
-- Using Self Join for this as parcel id is same for same address
-- ISNULL(check what is null, populate this with null)
select a.ParcelID, a.PropertyAddress, b.ParcelID, b.PropertyAddress, ISNULL(a.PropertyAddress, b.PropertyAddress)
from PortfolioProject.dbo.NashvilleHousing a
Join PortfolioProject.dbo.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.dbo.NashvilleHousing a
Join PortfolioProject.dbo.NashvilleHousing b
on a.ParcelID = b.ParcelID
AND a.[UniqueID ] <> b.[UniqueID ]
where a.PropertyAddress is Null
-- Breaking Out Address(Property and Owner) into indiviual columns (Address, City, State)
-- Breaking Out property address
select propertyaddress
from PortfolioProject.dbo.NashvilleHousing
-- Using Substring as the PropertyAddress is divided by a ',' with address and city
-- CharIndex('value', Column) is used to locate the value and prints out the len from start
select substring(PropertyAddress, 1, CharIndex(',', PropertyAddress) -1) as Address,
substring(PropertyAddress, CharIndex(',', PropertyAddress) +1, Len(PropertyAddress)) as City
from PortfolioProject.dbo.NashvilleHousing
alter table NashvilleHousing
Add SplitAddress nvarchar(255);
update NashvilleHousing
set SplitAddress = substring(PropertyAddress, 1, CharIndex(',', PropertyAddress) -1);
alter table NashvilleHousing
Add PropertyCity nvarchar(255);
update NashvilleHousing
set PropertyCity = substring(PropertyAddress, CharIndex(',', PropertyAddress) +1, Len(PropertyAddress));
select *
from PortfolioProject.dbo.NashvilleHousing
-- Breaking Out Owner Address Not using Substring but using ParseName(replace(), LastValue)
select
parsename(replace(OwnerAddress, ',' , '.'), 3),
parsename(replace(OwnerAddress, ',' , '.'), 2),
parsename(replace(OwnerAddress, ',' , '.'), 1)
from PortfolioProject.dbo.NashvilleHousing;
alter table NashvilleHousing
Add OwnerSplitAddress nvarchar(255);
update NashvilleHousing
set OwnerSplitAddress = parsename(replace(OwnerAddress, ',' , '.'), 3)
alter table NashvilleHousing
Add OwnerCity nvarchar(255);
update NashvilleHousing
set OwnerCity = parsename(replace(OwnerAddress, ',' , '.'), 2)
alter table NashvilleHousing
Add OwnerState nvarchar(255);
update NashvilleHousing
set OwnerState= parsename(replace(OwnerAddress, ',' , '.'), 1)
select *
from PortfolioProject.dbo.NashvilleHousing
-- Change Y and N to Yes and No in "Sold as Vacant" field
select Distinct(SoldAsVacant), count(SoldAsVacant)
from PortfolioProject.dbo.NashvilleHousing
group by SoldAsVacant
order by 2
select SoldAsVacant
, Case when SoldAsVacant = 'Y' then 'Yes'
when SoldAsVacant = 'N' then 'No'
else SoldAsVacant
end
as NewSoldAsVacant
from PortfolioProject.dbo.NashvilleHousing
update NashvilleHousing
set SoldAsVacant = Case when SoldAsVacant = 'Y' then 'Yes'
when SoldAsVacant = 'N' then 'No'
else SoldAsVacant
end
select *
from PortfolioProject.dbo.NashvilleHousing
-- Remove Duplication
-- Using windows functions
-- CTE : common table expression (CTE) is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. You can also use a CTE in a CREATE a view, as part of the view’s SELECT query. In addition, as of SQL Server 2008, you can add a CTE to the new MERGE statement.
With RowNumCTE AS (
select *,
ROW_NUMBER() over (
Partition by ParcelID,
PropertyAddress,
SalePrice,
SaleDate,
LegalReference
Order By
UniqueID
) row_num
from PortfolioProject.dbo.NashvilleHousing
-- order by ParcelID
)
select *
from RowNumCTE
where row_num > 1
order by propertyaddress
-- Deleting Unused Columns
select *
from PortfolioProject.dbo.NashvilleHousing
alter table PortfolioProject.dbo.NashvilleHousing
drop column owneraddress, taxdistrict, propertyaddress
alter table PortfolioProject.dbo.NashvilleHousing
drop column SaleDate