this first time posting stack overflow , know similar questions have been asked before, apologize in advance. however, can't seem work around regardless of solutions given in other forums.
i have 2 tables in sql server, 1 contains storm event info , other contains county info. both contain identical county_fips
numbers. neither of these contain primary or foreign keys.
i need replace nas storm event info lat/lons county table. possible without foreign/primary key relationships?
the stormevent
table looks this:
id | lat | lon | state_fips | county_fips ------------------------------------------ 1 | 33 | -88 | 028 | 087 2 | 31 | -98 | 048 | 225 3 | na | na | 017 | 034 4 | 39 | -100| 020 | 063
and on...
the countytable
looks (statefp10, countyfp10 = fips; intptlat10, intptlon10 = lat/lon):
statefp10 | county_fp10 | state_fp10| intptlat10 | intptlon10 -------------------------------------------------------------- 1 | 087 | 028 | 33 | -88 2 | 225 | 048 | 31 | -98 3 | 034 | 017 | 45 | -102
and on.
so far i've tried following code adjust lat column slight variations:
update n set n.lat= c.intptlat10 stormevent n inner join countytable c on n.county_fips= c.countyfp10 n.lat = 'na'
the query executed, , i'm told x amount of rows affected, when write select statement retrieve of nas included in lats stormevent
table, still there.
if can point me in right direction appreciated! again, apologize if going wrong way, first post , sql novice.
you may need update join
. i've created table variables test with.
declare @stormevent table (id int, lat varchar(10), lon varchar(10), state_fips int, county_fips int) insert @stormevent (id , lat, lon, state_fips, county_fips) values (1,'33','-88',028,087), (2,'31','-98',048,225), (3,'na','na',017,034), (4,'39','-100',020,063) declare @countytable table (statefp10 int, county_fp10 int, state_fp10 int, intptlat10 varchar(10), intptlon10 varchar(10)) insert @countytable values (1,087,028,'33','-88'), (2,225,048,'31','-98'), (3,034,017,'45','-102') update n set n.lat= c.intptlat10, n.lon = c.intptlon10 @stormevent n inner join @countytable c on n.state_fips= c.state_fp10 n.lat = 'na' select * @stormevent
assuming want both n/a
values updated, add lon
column update
.
set n.lat = c.intptlat10, n.lon = c.intptlon10
results:
id lat lon state_fips county_fips 1 33 -88 28 87 2 31 -98 48 225 3 45 -102 17 34 4 39 -100 20 63
Comments
Post a Comment