【问题标题】:Dividing monthly rate between Start and End Dates在开始日期和结束日期之间划分月费率
【发布时间】:2017-12-07 12:44:15
【问题描述】:

我在一个表中为每个员工设置了月费率,我需要根据特定项目中的员工开始和结束日期平均分配该表。 例如员工 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- 每月费用

因此,如果员工从 2017 年 11 月 4 日到 2017 年 12 月 31 日工作并且每月的成本是 4000 美元,那么在账单表中应该有 2 个条目,例如:

2017,Nov,Emp1,(4000/30)*((30-4)+1) 2017年12月Emp14000

我想根据表 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

【问题讨论】:

  • 添加一些示例表数据和预期结果 - 作为格式化文本(不是图像)。并向我们展示您当前的查询尝试。
  • 欢迎使用 StackOverflow,请参考stackoverflow.com/help/how-to-ask
  • startdate 获取月末(如果结束日期不是同一个月),获取天数。取enddate 并获取月初(如果开始日期早于enddate 月份),获取天数。计算每个月,(rate\#days in month) * calculated days
  • 添加了详细信息

标签: sql sql-server datetime


【解决方案1】:
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))))

【讨论】:

  • OP 希望按月细分成本。
  • 费率是月费率,不是日费率。因此,您每天都在计算月费率 *。我想要那张薪水! :-)
  • eomonth(sysdatetime()) 应该是 eomonth(startdate),这可以在下个月或任何时候运行。
  • @WEI_DBA 感谢指出!我没有做适当的测试,但它会给他工作的方向
猜你喜欢
  • 2012-05-31
  • 2020-12-17
  • 2020-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多