sql server中的工资计算

时间:2016-01-13 06:43:52

标签: sql sql-server sql-server-2008

您好我正在使用以下Sql语句来计算薪水

DECLARE @startDate DATETIME, @endDate DATETIME, @currentDate DATETIME, @currentDay INT, @PerDaycount INT, @Monthcount INT

 DECLARE @currentMonth INT, @lastDayOfStartMonth INT 
 CREATE TABLE #VacationDays ([Month] VARCHAR(10), [DaysSpent] INT,[MonthDays] VARCHAR(10),[PerdayAmt] decimal(8,2),[TotalAmt] decimal(8,2))

 DECLARE @Salary decimal(8,0)


 SET @Salary  = 8000

 SET @startDate = '01/01/2015'
 SET @endDate = '12/07/2015'
 SET @currentMonth = DATEPART(mm, @startDate)
 SET @currentDay = DATEPART(dd, @startDate)
 SET @currentDate = @startDate

 WHILE @currentMonth < DATEPART(mm, @endDate)
 BEGIN
    SELECT @lastDayOfStartMonth =
        DATEPART(dd, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@currentDate)+1,0)))
    PRINT @lastDayOfStartMonth
    INSERT INTO #VacationDays
    SELECT DATENAME(month, @currentDate) AS [Month],
        @lastDayOfStartMonth - @currentDay + 1 AS [DaysSpent],@lastDayOfStartMonth as a,@Salary/@lastDayOfStartMonth As dayammt,(@Salary/@lastDayOfStartMonth ) * @lastDayOfStartMonth - @currentDay + 1 AS totamt

    SET @currentDate = DATEADD(mm, 1, @currentDate)
    SET @currentMonth = @currentMonth + 1
    SET @currentDay = 1
 END

 IF DATEPART(mm, @startDate) = DATEPART(mm, @endDate)
 BEGIN
    INSERT INTO #VacationDays
    SELECT DATENAME(month, @endDate) AS [Month],
        DATEPART(dd, @endDate) - DATEPART(dd, @startDate) + 1 AS [DaysSpent],@lastDayOfStartMonth as a,@Salary/@lastDayOfStartMonth As dayammt,(@Salary/@lastDayOfStartMonth ) * DATEPART(dd, @endDate) - DATEPART(dd, @startDate) + 1 AS totamt
 END
 ELSE
 BEGIN
    INSERT INTO #VacationDays
    SELECT DATENAME(month, @endDate) AS [Month],
        DATEPART(dd, @endDate) AS [DaysSpent],@lastDayOfStartMonth as a,@Salary/@lastDayOfStartMonth As dayammt,(@Salary/@lastDayOfStartMonth ) * DATEPART(dd, @endDate) AS totamt
 END

 SELECT * FROM #VacationDays
 DROP TABLE #VacationDays

这是结果:

January     31  31  258.06  8000.00
February    28  28  285.71  8000.00
March       31  31  258.06  8000.00
April       30  30  266.67  8000.00
May         31  31  258.06  8000.00
June        30  30  266.67  8000.00
July        31  31  258.06  8000.00
August      31  31  258.06  8000.00
September   30  30  266.67  8000.00
October     31  31  258.06  8000.00
November    30  30  266.67  8000.00
December    7   30  266.67  1866.67

问题是有一天平日出现错误,例如dec。有31天但是有30天。

如何解决这个问题。

开始日期和结束日期可根据要求进行更改。

3 个答案:

答案 0 :(得分:1)

请在while循环执行后重新计算月份日,因为你的while循环不包括上个月,while循环后添加这一行

  SELECT @lastDayOfStartMonth =
    DATEPART(dd, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@currentDate)+1,0)))

答案 1 :(得分:1)

您好,您可以使用此查询。

    DECLARE @startDate DATETIME, @endDate DATETIME, @currentDate DATETIME, @currentDay INT, @PerDaycount INT, @Monthcount INT

   DECLARE @currentMonth INT, @lastDayOfStartMonth INT 
  CREATE TABLE #VacationDays ([Month] VARCHAR(10), [DaysSpent] INT,[MonthDays] VARCHAR(10),[PerdayAmt] decimal(8,2),[TotalAmt] decimal(8,2))

 DECLARE @Salary decimal(8,0)


 SET @Salary  = 8000

 SET @startDate = '01/01/2015'
  SET @endDate = '12/07/2015'
  SET @currentMonth = DATEPART(mm, @startDate)
 SET @currentDay = DATEPART(dd, @startDate)
 SET @currentDate = @startDate

 WHILE @currentMonth <= DATEPART(mm, @endDate)
BEGIN
  SELECT @lastDayOfStartMonth =
  DATEPART(dd, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@currentDate)+1,0)))
PRINT @lastDayOfStartMonth
IF(@currentMonth != DATEPART(mm, @endDate))
BEGIN
INSERT INTO #VacationDays
SELECT DATENAME(month, @currentDate) AS [Month],
    @lastDayOfStartMonth - @currentDay + 1 AS [DaysSpent],@lastDayOfStartMonth as a,@Salary/@lastDayOfStartMonth As dayammt,(@Salary/@lastDayOfStartMonth ) * @lastDayOfStartMonth - @currentDay + 1 AS totamt
END
ELSE
BEGIN
INSERT INTO #VacationDays
   SELECT DATENAME(month, @endDate) AS [Month],
   DATEPART(dd, @endDate) AS [DaysSpent],@lastDayOfStartMonth as a,@Salary/@lastDayOfStartMonth As dayammt,(@Salary/@lastDayOfStartMonth ) * DATEPART(dd, @endDate) AS totamt
END
SET @currentDate = DATEADD(mm, 1, @currentDate)
SET @currentMonth = @currentMonth + 1
SET @currentDay = 1
END

SELECT * FROM #VacationDays
DROP TABLE #VacationDays

它有点修改。

此处根据以下评论进行更改

更改了While

的条件
WHILE @currentMonth <= DATEPART(mm, @endDate) 

因为@lastDayOfStartMonth是在while循环内计算的。并且对于12月份它没有改变并且它选择了11月份的数据,即30个。所以我相应地改变它以在while循环中获得正确的数据。

答案 2 :(得分:1)

DECLARE @startDate DATETIME, @endDate DATETIME, @currentDate DATETIME, @currentDay INT, @PerDaycount INT, @Monthcount INT

DECLARE @currentMonth INT, @lastDayOfStartMonth INT 
CREATE TABLE #VacationDays ([Month] VARCHAR(10), [DaysSpent] INT,[MonthDays] VARCHAR(10),[PerdayAmt] decimal(8,2),[TotalAmt] decimal(8,2))

DECLARE @Salary decimal(8,0)


SET @Salary  = 8000

SET @startDate = '01/01/2015'
SET @endDate = '12/07/2015'
SET @currentMonth = DATEPART(mm, @startDate)
SET @currentDay = DATEPART(dd, @startDate)
SET @currentDate = @startDate

WHILE @currentMonth <= DATEPART(mm, @endDate)
BEGIN
SELECT @lastDayOfStartMonth =
    DATEPART(dd, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@currentDate)+1,0)))
PRINT @lastDayOfStartMonth
INSERT INTO #VacationDays
SELECT DATENAME(month, @currentDate) AS [Month],
    @lastDayOfStartMonth - @currentDay + 1 AS [DaysSpent],@lastDayOfStartMonth as a,@Salary/@lastDayOfStartMonth As dayammt,(@Salary/@lastDayOfStartMonth ) * @lastDayOfStartMonth - @currentDay + 1 AS totamt

SET @currentDate = DATEADD(mm, 1, @currentDate)
SET @currentMonth = @currentMonth + 1
SET @currentDay = 1
END

  IF DATEPART(mm, @startDate) = DATEPART(mm, @endDate)
   BEGIN
   INSERT INTO #VacationDays
   SELECT DATENAME(month, @endDate) AS [Month],
    DATEPART(dd, @endDate) - DATEPART(dd, @startDate) + 1 AS [DaysSpent],@lastDayOfStartMonth as a,@Salary/@lastDayOfStartMonth As dayammt,(@Salary/@lastDayOfStartMonth ) * DATEPART(dd, @endDate) - DATEPART(dd, @startDate) + 1 AS totamt
 END
 ELSE
 BEGIN
   INSERT INTO #VacationDays
   SELECT DATENAME(month, @endDate) AS [Month],
    DATEPART(dd, @endDate) AS [DaysSpent],@lastDayOfStartMonth as a,@Salary/@lastDayOfStartMonth As dayammt,(@Salary/@lastDayOfStartMonth ) * DATEPART(dd, @endDate) AS totamt
 END
  SELECT * FROM #VacationDays
  DROP TABLE #VacationDays

尝试此代码...并检查输出并且不要从输出中选择最后一行,无论此查询返回什么。