2016-01-13 72 views
1

嗨,我使用下面的SQL语句来这里计算工资工资计算在SQL Server

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 

的问题是有时候monthdays正在添加错了,因为在例如(分解)。有31天,但30天。

如何解决这个问题。

开始日期和结束日期可根据需要更改。

回答

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的条件。而对于十二月份来说,它并没有改变,它挑选11月份的数据是30。因此,我相应地改变了它,以便在while循环本身内获得正确的数据。

+1

你能否请你也解释*你做了什么/改变了? *这是有点修改*真的不是非常有用 - 你需要告诉**什么**被修改 - 以及为什么 –

+0

我改变了条件,而WHILE @currentMonth <= DATEPART(mm,@endDate)'因为@ lastDayOfStartMonth是在while循环内计算的。而对于十二月份来说,它并没有改变,它挑选11月份的数据是30。因此,我相应地改变了它,以便在while循环本身内获得正确的数据。 - 请检查 – Viplock

+0

@marc_s做的解决方案和解释为你工作? – Viplock

1

后while循环执行,因为while循环上个月不包括添加此行之后while循环

SELECT @lastDayOfStartMonth = 
    DATEPART(dd, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,@currentDate)+1,0))) 
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 

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

+0

你也可以请*解释*你做了什么/改变了?不要只是转储你的代码 - 告诉我们你正在做什么来实现结果! –

+0

WHILE @currentMonth ** <= ** DATEPART(mm,@endDate) –