SQLTeam.com | Weblogs | Forums

Based on codittion retrive data in sql server

sql2008
sql2012

#1

I have doubt in sql server

source: emp

id      | name  | check |deptname 
100     |  a    |  1    |ceo   
100     |  b    |  2    |hr
100     |  c    |  3    |po
100     |  d    |  5    |no
101     |  a    |  1    |pm
101     |  b    |  5    |ceo
102     |  a    |  1    |rn
102     |  b    |  2    |han


here same id have check 2 and 5 values then we need to replace  check values to 2 check values for that id.
based on above table I want load/output data into target table like below 
target : emp1

id      | name  | check |deptname
100     |  a    |  1    |ceo
100     |  d    |  2    |hr
100     |  c    |  3    |po
101     |  a    |  1    |pm
101     |  b    |  5    |ceo
102     |  a    |  1    |rn
102     |  b    |  2    |han

please tell me query how to achive this task in sql server


Need to Help query in sql server
Upda
#2

Same query as Check condition in sql server, just add this line to select section:

      ,isnull(a2.deptname,a1.deptname) as deptname