sql server 2012 - Change field when 2 conditions met -


i have timesheet database , need change typeofpay in following example

if person gets paid stat holiday (typeofpay = stat holiday) , worked on day (typeofpay = regular), change lines typeofpay = stat in.

  1. date description employee typeofpay timespent
  2. 2016-09-05 abc_company bobjones stat holiday 8.00
  3. 2016-09-05 bcd_company bobjones regular 1.25
  4. 2016-09-05 lift_check bobjones regular 0.25

thanks in advance gerry

this pretty simple update. join table using conditions supplied (same date, holiday vs regular, , same employee) , update type of pay. can see correlation doing join without update.

select *  #timesheet t left join #timesheet holiday on t.date = holiday.date , t.employee = holiday.employee , t.typeofpay = 'regular' , holiday.typeofpay = 'stat holiday' 

this table setup used. replace #timesheet whatever actual table name

create table #timesheet ( [date] date not null, [description] varchar(20) null, [employee] varchar(30) not null, [typeofpay] varchar(12) null, [timespent] numeric (12,2) null ) insert #timesheet  (date,description, employee, typeofpay, timespent) values ('2016-09-05', 'abc_company', 'bobjones', 'stat holiday', 8.00), ('2016-09-05', 'bcd_company', 'bobjones', 'regular' ,1.25), ('2016-09-05', 'lift_check', 'bobjones', 'regular', 0.25), ('2016-09-05', 'abc_company', 'bobjones', 'stat holiday', 8.00),  ('2016-09-05', 'bcd_company', 'bobjones', 'regular' ,1.25),  ('2016-09-05', 'lift_check', 'bobjones', 'regular', 0.25),  ('2016-09-05', 'lift_check', 'joesmith', 'regular', 0.25),  ('2016-09-06', 'lift_check', 'bobjones', 'regular', 0.25)    update t --(this timesheet table) set t.typeofpay = 'stat in'  #timesheet t inner join #timesheet holiday on t.date = holiday.date , t.employee = holiday.employee , t.typeofpay = 'regular' , holiday.typeofpay = 'stat holiday' t.typeofpay <> 'stat holiday'   

and can verify checking updated table.

select * #timesheet 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -