计算年薪

时间:2018-01-30 09:59:50

标签: sql-server

我有一张桌子,我在其中存储员工及其薪水的信息。我想从他/她被雇用之日起计算员工的年薪。我能够在一些人的帮助下得到我所需要的东西,但有一点我无法解决的问题是,如果一名员工在2018年1月被雇用而且他/她的薪水尚未得到处理#39;在月末结束后处理,但它显示了员工的工资。如果2017年雇用了同一名员工,那么就会给出完美的结果。

CREATE TABLE [dbo].[EmployeeInfo](
    [EmpID] [int] NOT NULL,
    [FirstName] [varchar](25) NULL,
    [LastName] [varchar](25) NULL,
    [MonthlySalary] [int] NULL,
    [DOJ] [date]
    )

数据如下:

INSERT INTO [dbo].[EmployeeInfo] VALUES (1, 'Ahmad', 'Usman', '20000', '2016-06-01')
INSERT INTO [dbo].[EmployeeInfo] VALUES (2, 'Erick', 'Ortiz', '18000', '2018-01-01')

我到目前为止已经得到了查询:

WITH cte AS (
    SELECT I.EmpID,
           I.FirstName,
           I.LastName,
           I.MonthlySalary,
           I.DOJ, 
           cntr = 1,
           SalaryMonth = DOJ
     FROM EmployeeInfo I 

     UNION ALL

     SELECT e.EmpID,
            e.FirstName,
            e.LastName,
            e.MonthlySalary,
            e.DOJ, 
            cntr = cntr + 1,
            Dateadd(month, cntr, e.DOJ)
     FROM  cte c
           JOIN EmployeeInfo e ON c.EmpID = e.EmpID
     WHERE  Dateadd(month, cntr + 1, e.DOJ) < DATEADD(dd,-(DAY(Getdate())),Getdate()))
SELECT EmpID,
       FirstName+' '+LastName [Employee Name],
       [DOJ],
       [Year] = Year(SalaryMonth),
       MonthlySalary, 
       YearlySalary = SUM(MonthlySalary),
       TotalSalary = Sum(Sum(MonthlySalary)) OVER(partition BY EmpID ORDER BY Year(SalaryMonth))
FROM cte
GROUP BY EmpID,
         Year(SalaryMonth),
         MonthlySalary,
         FirstName,
         LastName,
         [DOJ]
OPTION (MAXRECURSION 0);

任何帮助将不胜感激。请不要制作一个我可能根本无法理解的复杂的。

此致

2 个答案:

答案 0 :(得分:1)

您只需使用DATEDIFF

检查租用日期是否超过一个月
where DATEDIFF (month, DOJ, GETDATE()) > 1

以下是完整的工作代码:

WITH cte AS (
    SELECT I.EmpID,
           I.FirstName,
           I.LastName,
           I.MonthlySalary,
           I.DOJ, 
           cntr = 1,
           SalaryMonth = DOJ
     FROM EmployeeInfo I 

     UNION ALL

     SELECT e.EmpID,
            e.FirstName,
            e.LastName,
            e.MonthlySalary,
            e.DOJ, 
            cntr = cntr + 1,
            Dateadd(month, cntr, e.DOJ)
     FROM  cte c
           JOIN EmployeeInfo e ON c.EmpID = e.EmpID
     WHERE  Dateadd(month, cntr + 1, e.DOJ) < DATEADD(dd,-(DAY(Getdate())),Getdate()))
SELECT EmpID,
       FirstName+' '+LastName [Employee Name],
       [DOJ],
       [Year] = Year(SalaryMonth),
       MonthlySalary, 
       YearlySalary = SUM(MonthlySalary),
       TotalSalary = Sum(Sum(MonthlySalary)) OVER(partition BY EmpID ORDER BY Year(SalaryMonth))
FROM cte
WHERE DATEDIFF (month, DOJ, GETDATE()) > 1
GROUP BY EmpID,
         Year(SalaryMonth),
         MonthlySalary,
         FirstName,
         LastName,
         [DOJ]
OPTION (MAXRECURSION 0);

答案 1 :(得分:1)

这可能会让您获得更好的表现。请检查您的数据): -

declare @CurrentYear int;
declare @CurrentMonth int;
set @CurrentYear = Datepart(Year, GetDate()) ;
set @CurrentMonth = Datepart(Month,GetDate()) ;

with EmpYears AS (
select e.EmpID,
    e.FirstName,
    e.LastName,
    e.MonthlySalary,
    e.DOJ,
    @CurrentYear 'JYear',
    Case when datePart(Year, e.DOJ) = @CurrentYear 
        then ((@CurrentMonth  - datepart(month, e.DOJ)) * e.MonthlySalary )
        else (@CurrentMonth -1) * e.MonthlySalary  end 'YearlySalary'
from EmployeeInfo e 
UNION ALL
select e.EmpID,
    e.FirstName,
    e.LastName,
    e.MonthlySalary,
    e.DOJ,
    JYear - 1 'JYear',
    Case when datePart(Year, e.DOJ) = JYear - 1
        then (12 - datepart(month, e.DOJ) +1) * e.MonthlySalary
        else 12 * e.MonthlySalary  end 'YearlySalary'
from EmployeeInfo e
inner join EmpYears ey on e.EmpId = ey.EmpId where ey.JYear > DatePart(Year, e.DOJ)
)
    select EmpID,
   FirstName+' '+LastName [Employee Name],
   DOJ, JYear 'Year', MonthlySalary,
   Sum(YearlySalary) 'YearlySalary', 
   Sum(Sum(YearlySalary)) OVER(partition BY EmpID ORDER BY JYear) 'TotalSalary'
from EmpYears 
group by EmpID,
 FirstName,
 LastName,
 JYear, DOJ, MonthlySalary
order by EmpId, JYear