标签:出现 window where ret code select group by 员工 add
ROW_NUMBER() OVER(PARTITION BY 列名1 ORDER BY 列名2)
ROW_NUMBER()
是一个Window函数,它为结果集的分区中的每一行分配一个连续的整数。 行号以每个分区中第一行的行号开头。PARTITION BY
子句将结果集划分为分区。 ROW_NUMBER()
函数分别应用于每个分区,并重新初始化每个分区的行号。PARTITION BY
子句是可选的。如果未指定,ROW_NUMBER()
函数会将整个结果集视为单个分区。ORDER BY
子句定义结果集的每个分区中的行的逻辑顺序。 ORDER BY
子句是必需的,因为ROW_NUMBER()
函数对顺序敏感。-- =============================================
-- Author: temp
-- Create date: 2019-07-25
-- Description: 查当前员工当月连续请假超过5天的
-- =============================================
ALTER FUNCTION [dbo].[fn_ContinuationLeave]
(
@employeeCode VARCHAR(20) ,
@yearMonth VARCHAR(7)
)
RETURNS INT
AS
BEGIN
DECLARE @whether INT = 0;
DECLARE @startDate DATE ,
@endDate DATE;
SET @startDate = @yearMonth + '-01';
SET @endDate = CONVERT(VARCHAR(10), DATEADD(DAY, -1,
DATEADD(MONTH, 1,
@startDate)), 120);
SELECT @whether = COUNT(1)
FROM ( SELECT a1.EmployeeCode ,
MIN(ClassDate) startDate ,
MAX(ClassDate) endDate ,
COUNT(*) counts
FROM ( SELECT * ,
ClassDate
- ROW_NUMBER() OVER ( PARTITION BY EmployeeCode ORDER BY ClassDate ) re
FROM vwgCheckWorkQuery
WHERE SumLN = 1
AND SumLT >= 8
) a1
WHERE a1.ClassDate BETWEEN @startDate AND @endDate
AND a1.EmployeeCode = @employeeCode
GROUP BY a1.EmployeeCode ,
re
HAVING COUNT(1) >= 6 --将数据分组的对比后再分组,因为相差的间隔是一样,再根据差值一样就说明是无间隔连续的天数
) a;
RETURN @whether;
END;
标签:出现 window where ret code select group by 员工 add
原文地址:https://www.cnblogs.com/youMo/p/11271923.html