-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuery1.sql
175 lines (125 loc) · 4.83 KB
/
SQLQuery1.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
/*
Cleaning Data in SQL Queries
*/
Select * From Data_Cleaning.dbo.NashvilleHousing
/*Standardized Date Format
--By adding a column to the table called "SaleDateConverted" with the new date format.
Removing the old SaleDate Column
*/
Select SaleDateConverted, CONVERT(date,SaleDate)
From dbo.NashvilleHousing
Alter Table NashvilleHousing
Add SaleDateConverted Date;
Update NashvilleHousing
Set SaleDateConverted = CONVERT(date,SaleDate);
Alter Table NashvilleHousing
Drop Column SaleDate;
---------------------------------------------------------------------------------------------------------------------------------------------
/*Populate property Address Data
Using a Self Join to fill in null values in the property address with similar ParcelID
Using Isnull to fill in the Null Values within Property address
Then looping back into the Select statement to ensure that there are no more null values
*/
Select *
From NashvilleHousing
Where PropertyAddress is null
order by ParcelID
Select a.UniqueID, a.ParcelID, a.PropertyAddress, b.UniqueID, b.ParcelID, b.PropertyAddress, ISNULL(a.PropertyAddress,b.PropertyAddress)
From NashvilleHousing as a
Join NashvilleHousing as 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 as a
Join NashvilleHousing as 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)
On the PropertyAddress, by using Substrings and Charindex, split the address into address and city
Create new collumns for the new data set
*/
Select * from NashvilleHousing
Select
SUBSTRING(PropertyAddress,1,Charindex(',',PropertyAddress)-1) as Address,
SUBSTRING(PropertyAddress,Charindex(',',PropertyAddress)+1,len(PropertyAddress)) as City
From 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));
---------------------------------------------------------------------------------------------------------------------------------------------
/*
Breaking out Owners Address into Individual Columns (address, city, state)
This time using ParseName
Since ParseName takes in '.' instead of commas, use Replace to switch commas into periods.
Create new collumns for the new data set
*/
Select
PARSENAME(Replace(OwnerAddress,',','.'),3) as Address,
PARSENAME(Replace(OwnerAddress,',','.'),2) as City,
PARSENAME(Replace(OwnerAddress,',','.'),1) as State
From 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);
---------------------------------------------------------------------------------------------------------------------------------------------
/*
Change Y and N to Yes and NO in "Sold As Vacant"
using A case Statement to change Y to Yes and N to NO
*/
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
---------------------------------------------------------------------------------------------------------------------------------------------
/*
Removing Duplicates
*/
WITH RowNumCTE AS(
Select *,
ROW_NUMBER() Over(
Partition By ParcelID,
PropertyAddress,
SalePrice,
SaleDateConverted,
LegalReference
Order By
UniqueID
) row_num
From NashvilleHousing
)
Select *
From RowNumCTE
Where row_num >1
Order by PropertyAddress
--End
--------------------------------------------------------------------------------------------------------------------------------------------