sql server 2005 - MS SQL Update Query - Update all rows with that have the same field A to the top A value -
this should easy, i'm not getting it. have temp table i'm populating different sources. in temp table, have bunch of rows might have same subdivision name. if subdivision name same, want top latitude , longitude subdivision in rows subdivision. need subdivisions in temp table have isource = 1 or 2. isource field different sources. 0 reliable, 4th there not lat or long.
temp table
subdivisionname latitude longitude isource 100 200 0 100 200 0 102 200 2 b 104 202 1 b 105 203 1 b 106 202 2
desired result
subdivisionname latitude longitude isource 100 200 0 100 200 0 100 200 2 b 104 202 1 b 104 202 1 b 104 202 2
i tried this, sql server 2005 doesn't prefixes tt or ttt. can point me in right direction?
with #temptable ( select ttt.* , row_number() over(partition subdivisionname order isource) rnk #temptable ttt isource in (1,2) ) update #temptable set fieldsheetlat = ttt.fieldsheetlat, longitude = ttt.fieldsheetlong
;with cte ( select ttt.* , row_number() over(partition subdivisionname order isource) rnk #temptable ttt ) update tt set fieldsheetlat = cte.fieldsheetlat, longitude = cte.fieldsheetlong #temptable tt join cte on cte.subdivisionname = tt.subdivisionname , cte.rnk = 1 tt.isource > 0;
notes
- the last filter because there no never need update isource = 0 rows
- the update join pattern used here, source
cte rnk=1
, target#temptable
aliasedtt
Comments
Post a Comment