循环分组

时间:2018-08-11 10:47:22

标签: sql-server

ALTER FUNCTION [dbo].[fnCalTotalOvertime] 
    (@empCode INT, 
     @StartDate DATETIME, 
     @EndDate DATETIME) 
RETURNS INT
AS
BEGIN
    DECLARE  @Result INT = '0'

    WHILE @StartDate < @EndDate
    BEGIN
        SELECT @Result = @Result+SUM(ISNULL(CAST(LTRIM(DATEDIFF(MINUTE, 0, [dbo].[fnGetOTDuration](CAST (@empCode AS NVARCHAR), @StartDate))) AS INT), 0))

        SET @StartDate = @StartDate + 1;
    END;

    RETURN @Result
END

我有这个功能,可以在给定的日期内循环并计算加班的总和。现在有3种类型的加班时间,不同的费率。无论使用哪种类型,该函数都会循环并计算日期的总小时数,因此在尝试计算金额时不起作用。我希望它分组,但不要让我。我还有另一个获取日期类型的函数,但是当我分组使用它时会出现错误。

我尝试过这种方式...

ALTER FUNCTION [dbo].[fnCalTotalOvertime] 
    (@empCode INT, 
     @StartDate DATETIME, 
     @EndDate DATETIME) 
RETURNS INT
AS
BEGIN
    DECLARE  @Result INT = '0'

    WHILE @StartDate < @EndDate
    BEGIN
        SELECT @Result = @Result+SUM(ISNULL(CAST(LTRIM(DATEDIFF(MINUTE, 0, [dbo].[fnGetOTDuration](CAST (@empCode AS NVARCHAR), @StartDate))) AS INT), 0))
       GROUP BY [dbo].[fnGetDayType] (@StartDate, @empCode) 

       SET @StartDate = @StartDate + 1;
    END;

    RETURN @Result
END

任何指导吗?

0 个答案:

没有答案