如何在if ifif语句的sql中执行大小写

时间:2015-07-09 15:34:58

标签: sql

如果你在这里看到




  CREATE TABLE时间表
 ([username] varchar(31),[local_date] datetime,[hours] numeric,[wday] varchar(31))&#xA ;;

 INSERT INTO timesheet
 ([用户名],[local_date],[小时],[wday])
 VALUES
 ('emilioh@thinkpowersolutions.com','1915-05-24 19:00:00',3.75,'Sun'),
 ('emilioh@thinkpowersolutions.com','1915-05-25 19:00:00',11,'周一'),
 ('emilioh@thinkpowersolutions.com','1915-05-26 19:00:00',10.25,'星期二'),
 ('emilioh@thinkpowersolutions.com','1915-05-27 19:00:00',13,'Wed'),
 ('emilioh@thinkpowersolutions.com','1915-05-28 19:00:00',13,'星期四'),
 ('emilioh@thinkpowersolutions.com','1915-05-29 19:00:00',14,'Fri'),
 ('emilioh@thinkpowersolutions.com','1915-05-30 19:00:00',9,'星期六'),
 ('emilioh@thinkpowersolutions.com','1915-05-31 19:00:00',12,'Sun'),
 ('emilioh@thinkpowersolutions.com','1915-06-01 19:00:00',12.5,'周一')&#xA ;;

 select
用户名
 ,datepart(week,local_date)为Week
 ,总和(小时)总计
 ,总和(小时)< = 40然后总和(小时)其他40结束为Regulartime
 ,总和(小时)的情况> 40然后总和(小时) -  40其他0结束为Overtime
 from timeheet
 group by username,datepart(week,local_date);
  

& #xA;

SQL Fiddle


& #xA;

我有一个包含小时和天数的时间表表。&#xA;我需要计算常规时间。常规时间应该只是工作日时间,应该<= 40。如果它的周末或> 40然后加班。我如何在SQL中执行此操作?

&#xA;

4 个答案:

答案 0 :(得分:3)

<schemaFactory class="ClassicIndexSchemaFactory"/>

答案 1 :(得分:1)

如果我首先用CTE抽出周末/工作日部分,我发现更容易推断查询:

WITH HoursBuckets As
(
    SELECT username
        , DATEPART(week,local_date) As Week
        , SUM(hours) total
        , SUM(CASE WHEN DATENAME(dw, local_date) NOT IN ('Saturday', 'Sunday') THEN hours ELSE 0 END) As WeekDay
        , SUM(CASE WHEN DATENAME(dw, local_date) IN ('Saturday', 'Sunday') THEN hours ELSE 0 END) As WeekEnd
    FROM timesheet 
    GROUP BY username, DATEPART(week, local_date)
)
SELECT username, Week, Total,
   CASE WHEN Weekday > 40 THEN 40 ELSE Weekday END As RegularTime,
   CASE WHEN Weekday > 40 THEN Weekday - 40 ELSE 0 END + WeekEnd As OverTime
FROM HoursBuckets;

结果:

username                        Week    Total   RegularTime OverTime
emilioh@thinkpowersolutions.com 22      65      40          25
emilioh@thinkpowersolutions.com 23      34      25          9

答案 2 :(得分:0)

如果我理解正确,则RegularTime是M-F到最大40小时的总小时数 超时是总小时数 - 如果总小时数> 40则为40小时。 40.
因此,您不需要对加班进行一天的检查,因为如果他们在工作周内工作超过40小时,则额外的时间仍将被视为加班。

 select 
    username
    , datepart(week,local_date)  as Week
    , sum(hours) total
    , Case When sum(case when DATENAME(dw, local_date) NOT IN ('Saturday', 'Sunday') then hours else 0 end) <= 40
          Then sum(case when DATENAME(dw, local_date) NOT IN ('Saturday', 'Sunday') then hours else 0 end) Else 40 End as RegularTime
    , Case When sum(case when DATENAME(dw, local_date) NOT IN ('Saturday', 'Sunday') then hours else 0 end) > 40
          Then (sum(case when DATENAME(dw, local_date) NOT IN ('Saturday', 'Sunday') then hours else 0 end) - 40)
                + sum(case when DATENAME(dw, local_date) IN ('Saturday', 'Sunday') then hours else 0 end)
          Else sum(case when DATENAME(dw, local_date) IN ('Saturday', 'Sunday') then hours else 0 end) End as Overtime
from timesheet 
group by username, datepart(week, local_date);

输出:

username                        Week    total   RegularTime  OverTime
emilioh@thinkpowersolutions.com  22      65        40          25
emilioh@thinkpowersolutions.com  23      34        25           9

答案 3 :(得分:0)

此解决方案的不同之处仅在于它将所有步骤分解为子查询,以便您可以确切了解每个步骤的进展情况:

 CREATE TABLE timesheet
  ([username] varchar(31), [local_date] datetime, 
   [hours] numeric(6,2), [wday] varchar(31));

INSERT INTO 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, 
  week,
  sum_weekend_hours + sum_weekday_hours as total,
  case when sum_weekday_hours <= 40 then sum_weekday_hours else 40 end as Regulartime,
  sum_weekend_hours + 
     case when sum_weekday_hours <= 40 then 0 
     else sum_weekday_hours - 40  end as Overtime
from
  (select
    username, 
    Week,  
    sum(weekEndHours)  as sum_weekend_hours,
    sum(weekDayHours)  as sum_weekday_hours
  from 
    (select
        username,
        local_date,
        hours,
        wday,
        datepart(week,local_date) as Week, 
        datepart(weekday,local_date) as weekday,
        case when datepart(weekday,local_date) in (1,7) then hours else 0 end as weekEndHours,  -- sat, sun
        case when not(datepart(weekday,local_date) in (1,7)) then hours else 0 end as weekDayHours  -- sat, sun
      from 
        timesheet ) t
   group by
     username, 
     Week ) g;

SQL Fiddle