-
Notifications
You must be signed in to change notification settings - Fork 0
/
Housing_Project.sql
284 lines (185 loc) · 8.73 KB
/
Housing_Project.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
-- Choose Our Database
USE Housing;
-- Show Our Data
SELECT *
FROM Housing..Nashville;
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Show [Saledate] Column
SELECT SaleDate
FROM Housing..Nashville;
-- Convert [Saledate] Column Datatype into (Date)
-- In SQL Server, I can't directly change the Datatype of a column from a string (or another type) to a (date) Datatype using the [UPDATE] statement alone.
-- So, I creating a new column with the (date) Datatype, updating it with the converted values And drop my old column.
-- Step 1: Add a new column
ALTER TABLE Nashville
ADD SaleDateNew DATE;
-- Step 2: Update the new column with converted dates
UPDATE Nashville
SET SaleDateNew = CONVERT(DATE, SaleDate, 120);
-- Step 3: Drop the old column
ALTER TABLE Nashville
DROP COLUMN SaleDate;
-- Step 4: Rename the new column to the original name
EXEC sp_rename 'Nashville.SaleDateNew', 'SaleDate', 'COLUMN';
-- -- Show Our Converted Column
SELECT SaleDate
FROM Housing..Nashville;
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Checking if i have Nulls in [PropertyAddress] Column
SELECT PropertyAddress
FROM Housing..Nashville
WHERE PropertyAddress IS NULL;
-- After checking my data, I found few [ParcellID] is frequent, Therefore I have Nulls in [PropertyAddress] Column
-- So, I need to Populate these Nulls With these addresses that's already populated
-- So, I will solve it by self join
-- Show Nulls in [PropertyAddress] Column
SELECT a.ParcelID , a.PropertyAddress , b.ParcelID , b.PropertyAddress , ISNULL(a.PropertyAddress , b.PropertyAddress)
FROM Housing..Nashville a INNER JOIN Housing..Nashville b
ON a.ParcelID = b.ParcelID
AND a.[UniqueID ] <> B.[UniqueID ]
WHERE a.PropertyAddress IS NULL;
-- Replace Nulls in [PropertyAddress] Column by addresses that's already populated
UPDATE a
SET PropertyAddress = ISNULL(a.PropertyAddress , b.PropertyAddress)
FROM Housing..Nashville a INNER JOIN Housing..Nashville b
ON a.ParcelID = b.ParcelID
AND a.[UniqueID ] <> B.[UniqueID ]
WHERE a.PropertyAddress IS NULL;
-- Check Nulls again in [PropertyAddress] Column
SELECT a.ParcelID , a.PropertyAddress , b.ParcelID , b.PropertyAddress , ISNULL(a.PropertyAddress , b.PropertyAddress)
FROM Housing..Nashville a INNER JOIN Housing..Nashville b
ON a.ParcelID = b.ParcelID
AND a.[UniqueID ] <> B.[UniqueID ]
WHERE a.PropertyAddress IS NULL;
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Show [PropertyAddress] Column
SELECT PropertyAddress
FROM Housing..Nashville;
-- I notice that [PropertyAddress] is a Column have (2) Columns inside but Separated by Commas
-- So, I will break [PropertyAddress] Column into (2) Columns (Address , City)
-- So, I will solve it by Substring
-- I can Separate them like that
SELECT SUBSTRING(PropertyAddress , 1 , CHARINDEX(',' , PropertyAddress) -1) AS Property_Address ,
LTRIM(SUBSTRING(PropertyAddress , CHARINDEX(',' , PropertyAddress) +1 , LEN(PropertyAddress))) AS Property_City
FROM Housing..Nashville;
-- Or like that
SELECT LEFT(PropertyAddress, CHARINDEX(',', PropertyAddress) - 1) AS Property_Address,
LTRIM(SUBSTRING(PropertyAddress, CHARINDEX(',', PropertyAddress) + 1, LEN(PropertyAddress))) AS Property_City
FROM Housing..Nashville;
-- Or like that
SELECT LEFT(PropertyAddress, CHARINDEX(',', PropertyAddress) - 1) AS Property_Address,
SUBSTRING(PropertyAddress, CHARINDEX(',', PropertyAddress) + 2, LEN(PropertyAddress)) AS Property_City
FROM Housing..Nashville;
-- So, I add 2 New Columns to the table
ALTER TABLE Nashville
ADD Property_Address NVARCHAR(255), Property_City NVARCHAR(255);
-- Update the table with the split data
UPDATE Nashville
SET Property_Address = LEFT(PropertyAddress, CHARINDEX(',', PropertyAddress) - 1),
Property_City = LTRIM(SUBSTRING(PropertyAddress, CHARINDEX(',', PropertyAddress) + 1, LEN(PropertyAddress)));
-- Show [PropertyAddress] Column & Our 2 New Columns [Address , City]
SELECT PropertyAddress , Property_Address , Property_City
FROM Housing..Nashville;
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Show [OwnerAddress] Column
SELECT OwnerAddress
FROM Housing..Nashville;
-- I also notice that [OwnerAddress] is a Column have (3) Columns inside but Separated by Commas
-- So, I will break [OwnerAddress] Column into (3) Columns [Address , City, State]
-- So, I will solve it by Parsename
-- I can Separate them like that --> (Parsename : do with dots not Commas, so We replace Commas by dats first)
Select PARSENAME(REPLACE(OwnerAddress, ',', '.') , 3) AS Owner_Address ,
PARSENAME(REPLACE(OwnerAddress, ',', '.') , 2) AS Owner_City ,
PARSENAME(REPLACE(OwnerAddress, ',', '.') , 1) AS Owner_State
From Housing..Nashville;
-- So, I add 3 New Columns to the table
ALTER TABLE Nashville
ADD Owner_Address NVARCHAR(255), Owner_City NVARCHAR(255), Owner_State NVARCHAR(255);
-- Update the table with the split data
UPDATE Nashville
SET Owner_Address = PARSENAME(REPLACE(OwnerAddress, ',', '.') , 3) ,
Owner_City = PARSENAME(REPLACE(OwnerAddress, ',', '.') , 2) ,
Owner_State = PARSENAME(REPLACE(OwnerAddress, ',', '.') , 1)
-- Show [PropertyAddress] Column & Our 2 New Columns (Address , City)
SELECT OwnerAddress , Owner_Address , Owner_City , Owner_State
FROM Housing..Nashville;
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Show [SoldAsVacant] Column
SELECT SoldAsVacant
FROM Housing..Nashville;
-- Show Distinct Values in [SoldAsVacant] Column
SELECT DISTINCT(SoldAsVacant) , COUNT(SoldAsVacant)
FROM Housing..Nashville
GROUP BY SoldAsVacant
ORDER BY COUNT(SoldAsVacant);
-- So, I need to Change (Y and N) to (Yes and No) in [SoldAsVacant] Column
-- I will using Case statement
SELECT SoldAsVacant,
CASE
WHEN SoldAsVacant = 'Y' THEN 'Yes'
WHEN SoldAsVacant = 'N' THEN 'No'
ELSE SoldAsVacant
END AS New
FROM Housing..Nashville;
-- -- Update the table with these changes
UPDATE Nashville
SET SoldAsVacant = CASE
WHEN SoldAsVacant = 'Y' THEN 'Yes'
WHEN SoldAsVacant = 'N' THEN 'No'
ELSE SoldAsVacant
END;
-- Or i can Update the table by using (iif) function
UPDATE Nashville
SET SoldAsVacant = IIF(SoldAsVacant = 'Y', 'Yes', IIF(SoldAsVacant = 'N', 'No', SoldAsVacant));
-- Show Distinct Values in [SoldAsVacant] Column again to Check Changes
SELECT DISTINCT(SoldAsVacant) , COUNT(SoldAsVacant)
FROM Housing..Nashville
GROUP BY SoldAsVacant
ORDER BY COUNT(SoldAsVacant);
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Here, I want to Remove Duplicate rows
-- I solve it by using (CTE)
--Here, I can see all duplicated rows
WITH RowNumCTE AS(
Select *,
ROW_NUMBER() OVER (
PARTITION BY ParcelID , PropertyAddress , SalePrice , SaleDate , LegalReference
ORDER BY UniqueID
) row_num
From Housing..Nashville)
Select *
From RowNumCTE
Where row_num > 1
Order by PropertyAddress
-- Here, I will delete all duplicated rows
WITH RowNumCTE AS(
Select *,
ROW_NUMBER() OVER (
PARTITION BY ParcelID , PropertyAddress , SalePrice , SaleDate , LegalReference
ORDER BY UniqueID
) row_num
From Housing..Nashville)
DELETE
From RowNumCTE
Where row_num > 1
-- Check duplicated rows again
WITH RowNumCTE AS(
Select *,
ROW_NUMBER() OVER (
PARTITION BY ParcelID , PropertyAddress , SalePrice , SaleDate , LegalReference
ORDER BY UniqueID
) row_num
From Housing..Nashville)
Select *
From RowNumCTE
Where row_num > 1
Order by PropertyAddress
-------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Like i did before when I delete [SaleDate] after Creating new Column & put data on it
-- I will do this again & Delete Unused Columns
-- Delete Unused Columns
ALTER TABLE Housing..Nashville
DROP COLUMN PropertyAddress , OwnerAddress, TaxDistrict
-- Show Final view of my data
Select *
From Housing..Nashville