我应该如何使用条件乘法编写sql语句?

ruyhziif  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(382)

我有一张如下的table:
日程安排

+-------+------------+--------+----------------+-------+---------+
| index | date       | empid  | dept           | sfrom | slength |
+-------+------------+--------+----------------+-------+---------+
|     0 | 09-24-2018 | 943023 | RADIOLOGY      | 03PM  |       8 |
|     1 | 09-24-2018 | 891046 | SURGERY        | 07AM  |       8 |
|     2 | 09-24-2018 | 878397 | RADIOLOGY      | 11PM  |       8 |
|     3 | 09-24-2018 | 886190 | CARDIOLOGY     | 03PM  |       8 |
|     4 | 09-24-2018 | 878397 | RADIOLOGY      | 11PM  |       8 |
|     5 | 09-24-2018 | 891330 | SURGERY        | 03PM  |       8 |
|     6 | 09-24-2018 | 995561 | SURGERY        | 11PM  |       8 |
|     7 | 09-24-2018 | 967577 | MATERNITY      | 03PM  |       8 |
|     8 | 09-24-2018 | 913891 | PEDIATRICS     | 03PM  |       8 |
|     9 | 09-24-2018 | 939148 | EMERGENCY      | 07AM  |       8 |
|    10 | 09-24-2018 | 995636 | RADIOLOGY      | 11PM  |       8 |
|    11 | 09-24-2018 | 995561 | EMERGENCY      | 11PM  |       8 |
|    12 | 09-24-2018 | 967577 | SURGERY        | 07AM  |       8 |
|    13 | 09-24-2018 | 883069 | INTENSIVE CARE | 07AM  |       8 |
|    14 | 09-24-2018 | 951876 | ONCOLOGY       | 03PM  |       8 |
|    15 | 09-24-2018 | 884866 | CARDIOLOGY     | 07AM  |       8 |
|    16 | 09-24-2018 | 864425 | ONCOLOGY       | 03PM  |       8 |

员工表

+-------+--------+------------+-----------+---------+----------------+----------------+------+
| index | empid  | lastName   | firstName | emptype | cellphone      | homephone      | ftpt |
+-------+--------+------------+-----------+---------+----------------+----------------+------+
|     0 | 919675 | Cermak     | Vern      | LPN     | (138)7198-0862 | (655)2249-9926 | FT   |
|     1 | 906704 | Paille     | Lorilee   | NULL    | (858)1100-2722 | (377)1506-1986 | PT   |
|     2 | 900486 | Ober       | Shalanda  | NULL    | (210)1508-8132 | (820)1612-0197 | FT   |
|     3 | 883367 | Clyburn    | Jeffry    | LPN     | (420)7798-7220 | (476)7661-3070 | FT   |
|     4 | 953807 | Quarles    | Timmy     | LPN     | (513)2756-9892

如果我必须计算每个部门、每个员工类型和每个班次的总成本,我应该如何在sql中进行计算?
如果一次轮班包括午夜我的工资乘以25%?
我有一个示例输出,如下所示,但不知道如何做到这一点:

+------------+---------------+----------------+---------+
| DEPARTMENT | EMPLOYEE_TYPE |     SHIFT      |  COST   |
+------------+---------------+----------------+---------+
| Anesthesia | LPN           |    7 AM –3     | $24,567 |
| Anesthesia | LPN           |    3 PM –11PM  | $18,546 |
| Anesthesia | LPN           |    11 PM –7AM  | $22,874 |
| Anesthesia | NA            |    7 AM –3 PM  | $9,764  |
| Anesthesia | NA            |    3 PM –11 PM | $10,287 |
| Anesthesia | NA            |    11 PM –7 AM | $6, 875 |
| Anesthesia | RN            |    7 AM –3 PM  | $33,123 |
+------------+---------------+----------------+---------+

到目前为止,这是我的sql语句:但是我应该如何让它工作呢?编辑1:

Select 
      t1.dept,
      t2.emptype,
      t1.sfrom,
      t1.slength,
      SUM(t1.slength) as hours 
      CASE WHEN CAST(STR_TO_DATE(sfrom,'%h%p') + 
                     STR_TO_DATE(slength,'%H')) > STR_TO_DATE(23,'%H') 
      then (slength*10*1.25) else (slength*10) end as cost 
 from 
      schedule_2 as t1 
      Join (select 
                empid,
                emptype 
            from 
                employees_2) as t2 ON t1.empid=t2.empid 
 GROUP BY 
       dept,
       emptype,
       slength;

编辑2:

Select 
    t1.dept,
    t2.emptype,
    t1.sfrom,
    t1.slength,
    SUM(t1.slength) as hours
    CASE WHEN HOUR(STR_TO_DATE(sfrom,'%h%p') + 
                   STR_TO_DATE(slength,'%H')) > 23 
    then (slength*10*1.25) else (slength*10) 
from 
    schedule_2 as t1 
    Join (select 
               empid,
               emptype 
          from` 
               employees_2) as t2 ON t1.empid=t2.empid 
 GROUP BY 
      dept,
      emptype,
      slength;

我得到以下错误,但似乎无法找出原因?
sql语法有错误;检查与您的mariadb服务器版本相对应的手册,以获得正确的语法,使用“case when hour(str_to_date(sfrom,'%h%p')+str_to_date(slength,'%h'))”>23,然后在第2行使用“case when hour”

hivapdat

hivapdat1#

您可以在sql语句中使用case子句来表示成本字段。例如:

case when <<your clause for night shift>> then (slength*10)+(0.25*10*slength)
else (slength*10) end as cost

假设是每小时10美元。您可以根据您的业务规则设置夜班条件。

相关问题