Mapping SQL table -


sql server 2012: have table (cust_id int , p_id unique int, p_name, match_id, issue_date)

cust_id   p_id   p_name  match_id  issue_date 1         214    j5        1       2009-01-01 1         478    u3        1       2013-05-02  1         258    21        2       2003-04-05 1         369    65        2       2013-05-02 2         235    69        1       2011-05-09 2         897    36        1       2013-05-02 

now basically, rows 2,4 , 6 populated row 1, 3 , 5 , hence have same match_id , 2,4,6 have today’s date. want create mapping table follows:

i have table (custid, p_id, p_name, new_p_id, new_p_name)

cust_id   p_id   p_name   new_p_id   new_p_name 1         214     j5        478        u3    1         258     21        369        65    2         235     69        897        36    

please write me query, have tried many things sounds easy solution. bunch.

   insert #b  (cust_id ,   p_id ,   p_name, match_id )     select cust_id , p_id, p_name, match_id #a issue_date < getdate()     update #b     set new_p_id = new     ( select new_p_id, new = #a.p_id #b inner join #a on #a.cust_id = #b.cust_id , #a.match_id = #b.match_id ,  #a.p_id <> #b.p_id  ) #b      update #b     set new_p_name = new     ( select new_p_name, new = #a.p_name #b inner join #a on #a.cust_id = #b.cust_id , #a.match_id = #b.match_id ,  #a.p_id <> #b.p_id  ) #b 

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 -