-
Notifications
You must be signed in to change notification settings - Fork 0
/
joinNull_query2.sql
65 lines (58 loc) · 2.29 KB
/
joinNull_query2.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
-- 4. View 'OwnerName' col
-- First let me count how many null values there are
-- then I can view those null values for that column
select count(*)-count(OwnerName) as NullTotal
from nashville_housing.dbo.nash_data;
select *
from nashville_housing.dbo.nash_data
where OwnerName is null;
-- 5. Replacing null values in 'OwnerName' col
-- Want to chnage null values to "Unknown", instead of "NULL"
-- prefer to use the 'UniqueID' col becuae each null row has its
-- own unique identifier
select UniqueID,
OwnerName,
coalesce(OwnerName, 'UNKNOWN NAME') as NullNameValues
from nashville_housing.dbo.nash_data;
-- 6. Update 'OwnerName' col with the replaced null vals
-- had to use the entire database path b/c this
-- query is post cleaned data set.
-- typically, dbo.nash_data would have been okay
update nashville_housing.dbo.nash_data
set OwnerName = coalesce(OwnerName, 'UNKNOWN NAME');
-- 7. View 'PropertyAddress' and 'OwnerAddress' columns
-- Addresses are the same but for 'ownerAddress', it
-- includes the state abbreviations.
select PropertyAddress,
OwnerAddress
from nashville_housing.dbo.nash_data;
-- 8. View nulls with join 'PropertyAddress' *Don't RUN AFTER UPDATE*
-- Here, I am comparing the parcel IDs to nulls in 'PropertyAddress'
-- If the 'OwnerAddress' column did not include the state
-- I would've included it in this syntax. Also, the
-- 'ParcelID' is a good anchor becuase it is unique to
-- the property.
select nash1.ParcelID,
nash1.PropertyAddress,
nash2.ParcelID,
nash2.PropertyAddress,
isnull(nash1.PropertyAddress,
nash2.PropertyAddress)
from nashville_housing.dbo.nash_data nash1
join nashville_housing.dbo.nash_data nash2
on nash1.ParcelID = nash2.ParcelID
and nash1.[UniqueID ] <> nash2.[UniqueID ]
where nash1.PropertyAddress is null;
-- 9. Update populated 'PropertyAddress' *RUN ONCE*
update nash1
set PropertyAddress = isnull(nash1.PropertyAddress,
nash2.PropertyAddress)
from nashville_housing.dbo.nash_data nash1
join nashville_housing.dbo.nash_data nash2
on nash1.ParcelID = nash2.ParcelID
and nash1.[UniqueID ] <> nash2.[UniqueID ]
where nash1.PropertyAddress is null;
-- 10. View nulls with join 'OwnerAddress'
-- Can't use the same join method from 'PropertyAddress'.
-- will come back later to see how to address null values
-- in this col