-
Notifications
You must be signed in to change notification settings - Fork 0
/
NashvilleHousingSQL.sql
163 lines (114 loc) · 3.74 KB
/
NashvilleHousingSQL.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
--Cleaning data in SQL
Select *
From master.dbo.NashvilleHousing
-- Standardize Data Format
Select SaleDateConverted, Convert(Date,SaleDate)
From master.dbo.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
Select *
From master.dbo.NashvilleHousing
--Where PropertyAddress is null
order by ParcelID
Select a.ParcelID, a.PropertyAddress, b.ParcelID, b.PropertyAddress, ISNULL(a.PropertyAddress,b.PropertyAddress)
From master.dbo.NashvilleHousing a
Join master.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 master.dbo.NashvilleHousing a
Join master.dbo.NashvilleHousing b
on a.ParcelID = b.ParcelID
and a.[UniqueID] <> b.[UniqueID]
Where a.PropertyAddress is null
--Breaking out Address into Individual Cloumns (Address, City, State)
Select PropertyAddress
From master.dbo.NashvilleHousing
--Where PropertyAddress is null
--order by ParcelID
Select
Substring(PropertyAddress, 1,Charindex(',',PropertyAddress)-1) as Address,
Substring(PropertyAddress, Charindex(',', PropertyAddress) +1, Len(PropertyAddress)) as Address
From master.dbo.NashvilleHousing
Alter Table NashvilleHousing
Add PropertySplitAddress Nvarchar(255);
Update NashvilleHousing
Set PropertySplitAddress = Substring(PropertyAddress, 1,Charindex(',',PropertyAddress)-1)
Alter Table NashvilleHousing
Add PropertySplitCity Nvarchar(255);
Update NashvilleHousing
Set PropertySplitCity = Substring(PropertyAddress, Charindex(',', PropertyAddress) +1, Len(PropertyAddress))
Select *
From master.dbo.NashvilleHousing
Select OwnerAddress
From master.dbo.NashvilleHousing
Select
PARSENAME(Replace(OwnerAddress,',','.'),3),
PARSENAME(Replace(OwnerAddress,',','.'),2),
PARSENAME(Replace(OwnerAddress,',','.'),1)
From master.dbo.NashvilleHousing
Alter Table NashvilleHousing
Add OwnerSplitAddress Nvarchar(255);
Update NashvilleHousing
Set OwnerSplitAddress = PARSENAME(Replace(OwnerAddress,',','.'),3)
Alter Table NashvilleHousing
Add OwnerSplitCity Nvarchar(255);
Update NashvilleHousing
Set OwnerSplitCity = PARSENAME(Replace(OwnerAddress,',','.'),2)
Alter Table NashvilleHousing
Add OwnerSplitState Nvarchar(255);
Update NashvilleHousing
Set OwnerSplitState = PARSENAME(Replace(OwnerAddress,',','.'),1)
Select *
From master.dbo.NashvilleHousing
-- Change Y and N to Yes and No in "Sold as Vacant" field
Select Distinct(SoldAsVacant), Count(SoldAsVacant)
From master.dbo.NashvilleHousing
Group by SoldAsVacant
Order by 2
Select SoldAsVacant
, CASE When SoldAsVacant = 'Y' Then 'Yes'
When SoldAsVacant = 'N' Then 'No'
Else SoldAsVacant
END
From master.dbo.NashvilleHousing
Update NashvilleHousing
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 master.dbo.NashvilleHousing
--order by ParcelID
)
Delete
From RowNumCTE
Where row_num > 1
--order by PropertyAddress
Select *
From master.dbo.NashvilleHousing
-- Delete Unused Columns
Select *
From master.dbo.NashvilleHousing
Alter Table master.dbo.NashvilleHousing
Drop column OwnerAddress, TaxDistrict, PropertyAddress
Alter Table master.dbo.NashvilleHousing
Drop column SaleDate