-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLQuery (NashvilleHousing).sql
128 lines (102 loc) · 3.91 KB
/
SQLQuery (NashvilleHousing).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
--STANDARDIZE DATE FORMAT (2 versions)
--(versions: update/convert vs delete columns)
--Version 1 (update/convert)
update NashvilleHousing
set SaleDate = convert(date, saledate)
--Version 2 (delete columns)
alter table NashvilleHousing
add SaleDate2 date
update NashvilleHousing
set SaleDate2 = convert(date, saledate)
alter table NashvilleHousing
drop column SalesDate
-----------------------------------------------------------------------------------------------------------------------------------------------------------
--POPULATE PROPERTY ADDRESS DATA (self join)
-- test query, need to do a self join
select [UniqueID ], ParcelID
from NashvilleHousing
where ParcelID = ParcelID
--and [UniqueID ] != [UniqueID ]
group by [UniqueID ], ParcelID
--solution
select NashA.ParcelID, NashA.PropertyAddress, NashB.ParcelID, NashB.PropertyAddress, ISNULL(NashA.PropertyAddress, NashB.PropertyAddress)
from NashvilleHousing NashA
join NashvilleHousing NashB
on NashA.ParcelID = NashB.ParcelID
and NashA.[UniqueID ] != NashB.[UniqueID ]
where NashA.PropertyAddress is null
order by NashA.ParcelID
Update NashA
Set PropertyAddress = ISNULL(NashA.PropertyAddress, NashB.PropertyAddress)
from NashvilleHousing NashA
join NashvilleHousing NashB
on NashA.ParcelID = NashB.ParcelID
and NashA.[UniqueID ] != NashB.[UniqueID ]
-----------------------------------------------------------------------------------------------------------------------------------------------------------
--BREAKING OUT ADDRESS INTO INDIVIDUAL COLUMNS (ADDRESS, CITY, STATE)
--(2 versions: Substring vs ParseName)
--Version 1 (substring)
select
SUBSTRING(PropertyAddress, 1, CHARINDEX(',', propertyaddress) -1) as Address,
SUBSTRING(PropertyAddress, CHARINDEX(',', propertyaddress) +1, len(PropertyAddress)) as Address
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))
--Version 2 (parsename)
select
parsename(Replace(Owneraddress, ',', '.'), 3),
parsename(Replace(Owneraddress, ',', '.'), 2),
parsename(Replace(Owneraddress, ',', '.'), 1)
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" FIELD
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
-----------------------------------------------------------------------------------------------------------------------------------------------------------
--REMOVE DUPLICATES
With RowNumCTE as (
select *,
ROW_NUMBER() Over (
partition by ParcelID,
PropertyAddress,
SalePrice,
SaleDate,
LegalReference
Order by
UniqueID
) row_num
from NashvilleHousing
--order by ParcelID
)
DELETE
from RowNumCTE
where row_num > 1