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

  1. the last filter because there no never need update isource = 0 rows
  2. the update join pattern used here, source cte rnk=1 , target #temptable aliased tt

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -