-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuerydatacleanining.sql
162 lines (106 loc) · 4.07 KB
/
SQLQuerydatacleanining.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
select *
from PortfolioProject.dbo.NasvileHousing
--standardize the data format
select SaleDateConverted, CONVERT (Date, SaleDate)
from PortfolioProject.dbo.NasvileHousing
Update PortfolioProject.dbo.NasvileHousing
SET SaleDate = CONVERT (Date, SaleDate)
ALTER TABLE PortfolioProject.dbo.NasvileHousing
add SaleDateConverted Date;
Update PortfolioProject.dbo.NasvileHousing
SET SaleDateConverted = CONVERT (Date, SaleDate)
--populate property address data
select *
from PortfolioProject.dbo.NasvileHousing
--where PropertyAddress is null
order by ParcelID
select a.ParcelID, a.PropertyAddress, b.ParcelID, b.PropertyAddress, ISNULL(a.PropertyAddress, b.PropertyAddress)
from PortfolioProject.dbo.NasvileHousing a
JOIN PortfolioProject.dbo.NasvileHousing 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.NasvileHousing a
JOIN PortfolioProject.dbo.NasvileHousing b
ON a.parcelID = b.ParcelID
AND a.[UniqueID ] <> b.[UniqueID ]
where a.PropertyAddress is null
--Breaking out address into individual columns (address, city, state)
select PropertyAddress
from PortfolioProject.dbo.NasvileHousing
--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 PortfolioProject.dbo.NasvileHousing
ALTER TABLE PortfolioProject.dbo.NasvileHousing
add PropertySplitAddress Nvarchar(255);
Update PortfolioProject.dbo.NasvileHousing
SET PropertySplitAddress = SUBSTRING (PropertyAddress, 1, CHARINDEX(',', PropertyAddress) -1)
ALTER TABLE PortfolioProject.dbo.NasvileHousing
add PropertySplitCity Nvarchar(255);
Update PortfolioProject.dbo.NasvileHousing
SET PropertySplitCity = SUBSTRING (PropertyAddress, CHARINDEX(',', PropertyAddress) +1, len(PropertyAddress))
select *
from PortfolioProject.dbo.NasvileHousing
select OwnerAddress
from PortfolioProject.dbo.NasvileHousing
select PARSENAME(REPLACE(OwnerAddress, ',', '.'), 3)
, PARSENAME(REPLACE(OwnerAddress, ',', '.'), 2)
, PARSENAME(REPLACE(OwnerAddress, ',', '.'), 1)
from PortfolioProject.dbo.NasvileHousing
ALTER TABLE PortfolioProject.dbo.NasvileHousing
add OwnerSplitAddress Nvarchar(255);
Update PortfolioProject.dbo.NasvileHousing
SET OwnerSplitAddress = PARSENAME(REPLACE(OwnerAddress, ',', '.'), 3)
ALTER TABLE PortfolioProject.dbo.NasvileHousing
add OwnerSplitCity Nvarchar(255);
Update PortfolioProject.dbo.NasvileHousing
SET OwnerSplitCity = PARSENAME(REPLACE(OwnerAddress, ',', '.'), 2)
ALTER TABLE PortfolioProject.dbo.NasvileHousing
add OwnerSplitState Nvarchar(255);
Update PortfolioProject.dbo.NasvileHousing
SET OwnerSplitState = PARSENAME(REPLACE(OwnerAddress, ',', '.'), 1)
select *
from PortfolioProject.dbo.NasvileHousing
--change Y and N to Yes and No in the sold vacant field
select DISTINCT(SoldAsVacant), count(SoldAsVacant)
from PortfolioProject.dbo.NasvileHousing
group by SoldAsVacant
order by 2
select SoldAsVacant
, CASE when SoldAsVacant = 'Y' THEN 'Yes'
when SoldAsVacant = 'N' THEN 'No'
ELSE SoldAsVacant
END
from PortfolioProject.dbo.NasvileHousing
--Remove Duplicates
WITH RowNumCTE AS(
select *,
ROW_NUMBER() OVER (
PARTITION BY ParcelID,
PropertyAddress,
SalePrice,
SaleDate,
LegalReference
ORDER BY
UniqueID) row_num
from PortfolioProject.dbo.NasvileHousing
)
select *
from RowNumCTE
where row_num > 1
order by PropertyAddress
update PortfolioProject.dbo.NasvileHousing
Set SoldAsVacant = CASE when SoldAsVacant = 'Y' THEN 'Yes'
when SoldAsVacant = 'N' THEN 'No'
ELSE SoldAsVacant
END
--Delete Unused column
select *
from PortfolioProject.dbo.NasvileHousing
ALTER TABLE PortfolioProject.dbo.NasvileHousing
DROP COLUMN OwnerAddress, TaxDistrict, PropertyAddress