-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNashvilleHousing.sql
126 lines (98 loc) · 3.41 KB
/
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
--change date format
select SaleDate, convert(date, saledate) as newdate
from PortfolioProject..NashvilleHousing
update NashvilleHousing
set saledate = convert(date, saledate)
alter table NashvilleHousing
Add saledateconverted date;
update NashvilleHousing
set saledateconverted = convert(date, saledate)
select saledateconverted
from NashvilleHousing
--property address
select *
from PortfolioProject..NashvilleHousing
select a.ParcelID, a.propertyaddress, b.parcelid, b.PropertyAddress , ISNULL(a.propertyaddress, b.PropertyAddress)
from PortfolioProject.dbo.NashvilleHousing a
join PortfolioProject.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 PortfolioProject.dbo.NashvilleHousing a
join PortfolioProject.dbo.NashvilleHousing b
on a.ParcelID = b.ParcelID
and a.[UniqueID ] <> b.[UniqueID ]
where a.PropertyAddress is null
--breaking out address
select PropertyAddress
from PortfolioProject..NashvilleHousing
select
substring(propertyaddress, 1, charindex(',', propertyaddress) -1) as address
, substring(propertyaddress, charindex(',', propertyaddress) +1,LEN(Propertyaddress)) as address2
from PortfolioProject..NashvilleHousing
--create new columns
alter table PortfolioProject..NashvilleHousing
Add PropertySplitAddress Nvarchar(255);
update PortfolioProject..NashvilleHousing
set PropertySplitAddress = substring(propertyaddress, 1, charindex(',', propertyaddress) -1)
alter table PortfolioProject..NasvilleHousing
Add PropertySplitCity Nvarchar(255);
update PortfolioProject..NashvilleHousing
set PropertySplitCity = substring(propertyaddress, charindex(',', propertyaddress) +1,LEN(Propertyaddress))
select *
from PortfolioProject..NashvilleHousing
--split owneraddress
select
PARSENAME(REPLACE(owneraddress,',','.'),3),
PARSENAME(REPLACE(owneraddress,',','.'),2),
PARSENAME(REPLACE(owneraddress,',','.'),1)
from PortfolioProject..NashvilleHousing
Alter table PortfolioProject..NashvilleHousing
add OwnerSplitAddress nvarchar(255), OwnerSplitCity nvarchar(255), OwnersplitState nvarchar(255)
update PortfolioProject..NashvilleHousing
set OwnerSplitAddress = PARSENAME(REPLACE(owneraddress,',','.'),3),
OwnerSplitCity = PARSENAME(REPLACE(owneraddress,',','.'),2),
OwnersplitState = PARSENAME(REPLACE(owneraddress,',','.'),1)
select distinct(SoldAsVacant), count(SoldAsVacant)
from PortfolioProject..NashvilleHousing
group by SoldAsVacant
order by 2
select SoldAsVacant,
case when soldasvacant = 'Y' then 'Yes'
when soldasvacant = 'N' then 'No'
else soldasvacant
End
from PortfolioProject..NashvilleHousing
update PortfolioProject..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 PortfolioProject..NashvilleHousing
)
--select *
--from RowNumCTE
--where row_num >1
--order by PropertyAddress
delete
from RowNumCTE
where row_num >1
--delete columns
alter table PortfolioProject..NashvilleHousing
drop column saledate
select *
from PortfolioProject..NashvilleHousing