在开始日期和结束日期之间划分每月费率

时间:2017-12-07 12:44:15

标签: sql sql-server datetime

我有一个表中每个员工的月费率,我需要根据特定项目中的员工开始和结束日期平均分配。 对于例如员工X的月费率为4300美元,存储在表A中。员工开始时间为&该项目的结束日期为2017-11-03' &安培; ' 2017年12月12日'分别存储在表B中。我需要11月和12月的员工成本。

感谢您的任何帮助。

编辑:

表1:员工详细信息 Col1-员工姓名 Col2-开始日期 Col3-结束日期

表2:价目表 COL1地点 COL2级 Col3-每月费用

表3:员工开票 COL1年 COL2个月 Col3-员工 Col4-每月费用

因此,如果员工从11月4日 - 17日到31日 - 12月17日工作,每月费用为4000美元,那么在结算表中应该有2个条目,如:

2017 11月,EMP1,(三十〇分之四千)*((30-4)+1) 2017年,月,Emp1,4000

我很乐意根据表1中的输入在表3中插入2个条目,但无法计算自定义成本

case 
       when (webResource_Details_ESS_recurrent_entries.recurrent_month<>DATEPART(mm, l.start_date) or
      webResource_Details_ESS_recurrent_entries.recurrent_month<>DATEPART(mm, l.end_date))
      then
       l.Employee_Cost

       when DATEPART(day, l.start_date) <> 1 and Eomonth(l.start_date) = Eomonth(l.end_date)  then 
        (Datediff(day, l.start_date, l.end_date) *
            (l.Employee_Cost / Datediff(day,Eomonth(l.start_date), Dateadd(month,1, Eomonth(l.start_date))) ) ) 
       when  DATEPART(day, l.start_date) <> 1 and Eomonth(l.start_date) <> Eomonth(l.end_date)  then ( Datediff(day, l.start_date, Eomonth(l.start_date)) * 
            ( l.Employee_Cost /Datediff(day, Eomonth(l.start_date), Dateadd(month, 1,Eomonth(l.start_date))) ) )
            when DATEPART(day, l.end_date) <> (DATEPART(day, DATEADD(second,-1,DATEADD(month, DATEDIFF(month,0,l.end_date)+1,0)))) and Eomonth(l.start_date) <> Eomonth(l.end_date) then (Datediff(day, ( Dateadd(month, Datediff(month, 0,l.end_date),0) ),l.end_date)
            *(l.Employee_Cost /Datediff(day, Eomonth(l.end_date),Dateadd(month, 1, Eomonth(l.end_date)))))

      end as Employee_Cost

1 个答案:

答案 0 :(得分:0)

SELECT em.employeed, 
       CASE 
         WHEN Eomonth(startdate) = Eomonth(enddate) 
         THEN   (Datediff(day, startdate, enddate) *
                (rate / Datediff(day,Eomonth(startdate), Dateadd(month,1, Eomonth(startdate))) ) ) 
         ELSE   ( ( Datediff(day, startdate, Eomonth(startdate)) * 
                ( rate /Datediff(day, Eomonth(startdate), Dateadd(month, 1,Eomonth(startdate))) ) ) + 
                (Datediff(day, ( Dateadd(month, Datediff(month, 0,enddate),0) ),enddate)
                *(rate /Datediff(day, Eomonth(enddate),Dateadd(month, 1, Eomonth(enddate)))))) 
       END AS wage 
FROM   employeemaster em 
INNER JOIN employeerate AS er 
ON ( em.employeed = er.employeed ) 

First Case将处理开始日期和结束日期都在同一个月的情况

eomonth(startdate) = eomonth(enddate) then (datediff(day,startdate,enddate)*(rate/datediff(day, eomonth(startdate), dateadd(month, 1, eomonth(startdate)))))

计算开始日期的天数和费率

datediff(day,startdate,eomonth(startdate)) * (rate/datediff(day, eomonth(startdate), dateadd(month, 1, eomonth(startdate)))))

计算结束日期的天数和费率

(datediff(day,(DATEADD(month, DATEDIFF(month, 0, enddate), 0)),enddate)* rate/datediff(day, eomonth(enddate), dateadd(month, 1, eomonth(enddate))))
相关问题