How to do case in sql like if elseif statement -


if see here

create table timesheet     ([username] varchar(31), [local_date] datetime, [hours] numeric, [wday] varchar(31)) ;  insert timesheet     ([username], [local_date], [hours],[wday]) values     ('emilioh@thinkpowersolutions.com', '1915-05-24 19:00:00', 3.75,'sun'),     ('emilioh@thinkpowersolutions.com', '1915-05-25 19:00:00', 11,'mon'),     ('emilioh@thinkpowersolutions.com', '1915-05-26 19:00:00', 10.25,'tue'),     ('emilioh@thinkpowersolutions.com', '1915-05-27 19:00:00', 13,'wed'),     ('emilioh@thinkpowersolutions.com', '1915-05-28 19:00:00', 13,'thu'),     ('emilioh@thinkpowersolutions.com', '1915-05-29 19:00:00', 14,'fri'),     ('emilioh@thinkpowersolutions.com', '1915-05-30 19:00:00', 9,'sat'),     ('emilioh@thinkpowersolutions.com', '1915-05-31 19:00:00', 12,'sun'),     ('emilioh@thinkpowersolutions.com', '1915-06-01 19:00:00', 12.5,'mon') ;  select      username     , datepart(week,local_date)  week     , sum(hours) total     , case when sum(hours) <= 40 sum(hours) else 40 end regulartime     , case when sum(hours)  > 40 sum(hours) - 40 else 0 end overtime timesheet  group username, datepart(week, local_date); 

sql fiddle

i have timesheet table hours , days. need calculate regular time. regular time should weekday hours , should <=40. if weekend or >40 overtime. how do in sql?

    create table #timesheet         ([username] varchar(31), [local_date] datetime, [hours] numeric(6,2), [wday] varchar(31))      insert #timesheet         ([username], [local_date], [hours],[wday])     values         ('emilioh@thinkpowersolutions.com', '2015-05-24 19:00:00', 3.75 ,'sun'),         ('emilioh@thinkpowersolutions.com', '2015-05-25 19:00:00', 11   ,'mon'),         ('emilioh@thinkpowersolutions.com', '2015-05-26 19:00:00', 10.25,'tue'),         ('emilioh@thinkpowersolutions.com', '2015-05-27 19:00:00', 13   ,'wed'),         ('emilioh@thinkpowersolutions.com', '2015-05-28 19:00:00', 13   ,'thu'),         ('emilioh@thinkpowersolutions.com', '2015-05-29 19:00:00', 14   ,'fri'),         ('emilioh@thinkpowersolutions.com', '2015-05-30 19:00:00', 9    ,'sat'),             ('emilioh@thinkpowersolutions.com', '2015-05-31 19:00:00', 12   ,'sun'),         ('emilioh@thinkpowersolutions.com', '2015-06-01 19:00:00', 12.5 ,'mon')      select          username         , datepart(week,local_date)  week         , sum(hours) total         , case when sum(case when datename(dw, local_date) not in ('saturday', 'sunday') hours else 0 end) > 40                  40                 else sum(case when datename(dw, local_date) not in ('saturday', 'sunday') hours else 0 end) end reghours         , sum(case when datename(dw, local_date) in ('saturday', 'sunday') hours else 0 end) +              case when sum(case when datename(dw, local_date) not in ('saturday', 'sunday') hours else 0 end) > 40                  sum(case when datename(dw, local_date) not in ('saturday', 'sunday') hours else 0 end) - 40 else 0 end othours     #timesheet      group username, datepart(week, local_date)      drop table #timesheet  username                       week total   reghours    othours emilioh@thinkpowersolutions.com 22  74.00   40.00       34.00 emilioh@thinkpowersolutions.com 23  24.50   12.50       12.00 

Comments