Microsoft Access查询,使用ROW_NUMBER的替代方式重复日期

时间:2019-03-29 01:13:37

标签: sql ms-access

很显然,MS Access中有一堆关于ROW_NUMBER的问题,通常的回答是它不存在,而是使用COUNT(*)创建类似的东西。不幸的是,这样做并没有给我所需的结果。

我的数据如下:

employee_id -------start_time ------- start_date
000670 -----------------12:30 ------------ 2019/03/28
000670 -----------------11:22 ------------ 2019/03/30
000676 -----------------13:00 ------------ 2019/03/28
000676 -----------------11:30 ------------ 2019/03/29
000676 -----------------14:00 ------------ 2019/03/29
000676 -----------------11:20 ------------ 2019/03/30
000676 -----------------14:00 ------------ 2019/03/30
000676 -----------------11:00 ------------ 2019/03/31
000677 -----------------11:20 ------------ 2019/03/28
000677 -----------------12:20 ------------ 2019/03/29
000678 -----------------12:00 ------------ 2019/03/29

使用COUNT(*)我得到:

employee_id -------- start_date ------- seq
000670  ------------ 2019/03/28 ------- 1
000670  ------------ 2019/03/30 ------- 2
000676  ------------ 2019/03/28 ------- 1
000676  ------------ 2019/03/29 ------- 3
000676  ------------ 2019/03/29 ------- 3
000676  ------------ 2019/03/30 ------- 5
000676  ------------ 2019/03/30 ------- 5
000676  ------------ 2019/03/31 ------- 6
000677  ------------ 2019/03/28 ------- 1
000677  ------------ 2019/03/29 ------- 2
000678  ------------ 2019/03/29 ------- 1

我当前的查询是:

SELECT tbl.employee_id
     , tbl.start_date
     , ( SELECT COUNT(*)
           FROM tbl AS tbl2 
          WHERE tbl2.employee_id = tbl.employee_id 
            AND tbl2.start_date <= tbl.start_date ) AS seq
  FROM tbl
ORDER BY tbl.employee_id, tbl.start_date

我想了解的是对employee_id和start_date的唯一计数,以便我的查询输出如下:

employee_id -------- start_date ------- seq
000670  ------------ 2019/03/28 ------- 1
000670  ------------ 2019/03/30 ------- 2
000676  ------------ 2019/03/28 ------- 1
000676  ------------ 2019/03/29 ------- 2
000676  ------------ 2019/03/29 ------- 3
000676  ------------ 2019/03/30 ------- 4
000676  ------------ 2019/03/30 ------- 5
000676  ------------ 2019/03/31 ------- 6
000677  ------------ 2019/03/28 ------- 1
000677  ------------ 2019/03/29 ------- 2
000678  ------------ 2019/03/29 ------- 1

1 个答案:

答案 0 :(得分:1)

请尝试这个。

对于SQL

SELECT 
    tbl.employee_id, tbl.start_date,
    ROW_NUMBER() OVER(PARTITION BY employee_id ORDER BY employee_id)As Row_Num 
FROM tbl

用于MS访问

SELECT tbl.employee_id
     , tbl.start_date
     , ( SELECT COUNT(*)
           FROM tbl AS tbl2 
          WHERE tbl2.employee_id = tbl.employee_id 
         AND  tbl2.start_date+tbl2.start_time < tbl.start_date+tbl.start_time
         )+1 AS seq
  FROM tbl