SQL 查询以查找员工的相同缺勤次数

SQL query to find the count of number of same absences for an employee

我有一个 table:

PERSON_NUMBER       ABS_DATE            ABS_TYPE_NAME               ABS_DAYS
-----------------------------------------------------------------------------
1010            01-01-2022              PTO                             1
1010            01-01-2022              PTO                             1
1010            06-01-2022              PTO                             0.52
1020            02-02-2022              VACATION                        1
1020            02-02-2022              VACATION                        0.2
1030            01-12-2021              PTO                             1
1030            01-12-2021              PTO                             1
1040            02-12-2021              sick                            1                   
1040            30-12-2021              sick                            1
1050            30-01-2022              SICK                            1

我想在输出中添加另一列 COUNT,它告诉我一个人在同一日期有相同 ABS_TYPE 的重复数据的实例。

PERSON_NUMBER       ABS_DATE            ABS_TYPE_NAME               ABS_DAYS            COUNT
------------------------------------------------------------------------------
1010            01-01-2022              PTO                             1                2
1010            01-01-2022              PTO                             1                           
1010            06-01-2022              PTO                             0.52             1 
1020            02-02-2022              VACATION                        1                2       
1020            02-02-2022              VACATION                        1                
1030            01-12-2021              PTO                             1                2 
1030            01-12-2021              PTO                             1 
1040            02-12-2021              sick                            1                1                    
1040            30-12-2021              sick                            1                1 
1050            30-01-2022              SICK                            1                1

我正在使用-

COUNT(ABS_DATE) OVER (PARTITION BY ABS_DATE, PERSON_NUMBER, ABS_TYPE_NAME 
                      ORDER BY PERSON_NUMBER, ABS_TYPE_NAME)

但是这将返回第一行的输出 4。它还为所有行返回。我想要这些记录的一个值。

例如-如果 2 出现在第一行,它不应该出现在第二行

当您使用分区子句执行 COUNT 时,不需要 ORDER BY,因为记录的计数顺序无关紧要。

您还可以使用第二个分析函数来确定将包含“计数”的第一行,并仅在结果中显示该行。

查询

WITH
    absences (PERSON_NUMBER,
              ABS_DATE,
              ABS_TYPE_NAME,
              ABS_DAYS)
    AS
        (SELECT 1010, DATE '2022-01-01', 'PTO', 1 FROM DUAL
         UNION ALL
         SELECT 1010, DATE '2022-01-01', 'PTO', 1 FROM DUAL
         UNION ALL
         SELECT 1010, DATE '2022-01-06', 'PTO', 0.52 FROM DUAL
         UNION ALL
         SELECT 1020, DATE '2022-02-02', 'VACATION', 1 FROM DUAL
         UNION ALL
         SELECT 1020, DATE '2022-02-02', 'VACATION', 0.2 FROM DUAL
         UNION ALL
         SELECT 1030, DATE '2021-12-01', 'PTO', 1 FROM DUAL
         UNION ALL
         SELECT 1030, DATE '2021-12-01', 'PTO', 1 FROM DUAL
         UNION ALL
         SELECT 1040, DATE '2021-12-02', 'sick', 1 FROM DUAL
         UNION ALL
         SELECT 1040, DATE '2021-12-30', 'sick', 1 FROM DUAL
         UNION ALL
         SELECT 1050, DATE '2022-01-30', 'SICK', 1 FROM DUAL)
  SELECT person_number,
         abs_date,
         abs_type_name,
         abs_days,
         CASE rn WHEN 1 THEN cnt END     AS COUNT
    FROM (SELECT a.*,
                 COUNT (ABS_DATE) OVER (PARTITION BY ABS_DATE, PERSON_NUMBER, ABS_TYPE_NAME)
                     AS cnt,
                 ROW_NUMBER () OVER (PARTITION BY ABS_DATE, PERSON_NUMBER, ABS_TYPE_NAME ORDER BY 1)
                     AS rn
            FROM absences a)
ORDER BY person_number, abs_type_name, abs_date, rn;

结果

   PERSON_NUMBER     ABS_DATE    ABS_TYPE_NAME    ABS_DAYS    COUNT
________________ ____________ ________________ ___________ ________
            1010 01-JAN-22    PTO                        1        2
            1010 01-JAN-22    PTO                        1
            1010 06-JAN-22    PTO                     0.52        1
            1020 02-FEB-22    VACATION                   1        2
            1020 02-FEB-22    VACATION                 0.2
            1030 01-DEC-21    PTO                        1        2
            1030 01-DEC-21    PTO                        1
            1040 02-DEC-21    sick                       1        1
            1040 30-DEC-21    sick                       1        1
            1050 30-JAN-22    SICK                       1        1