时间范围之间的过滤和组结果

时间:2018-03-06 03:33:29

标签: sql ms-access group-by

  1. timesetup(非标准化),此表用于过滤结果的时间条件

    | time_id | period_from | period_to  | session1_from | session1_to | session2_from | session2_to | session3_from | session3_to | session4_from | session4_to | session5_from | session5_to |
    |---------|-------------|------------|---------------|-------------|---------------|-------------|---------------|-------------|---------------|-------------|---------------|-------------|
    | 1       | 10/09/2015  | 11/09/2015 | 04:00:00      | 05:00:00    | 12:00:00      | 13:00:00    | 15:00:00      | 16:00:00    | 18:00:00      | 18:35:00    | 19:00:00      | 20:00:00    |
    | 2       | 12/09/2015  | 13/09/2015 | 04:10:00      | 05:10:00    | 12:10:00      | 13:10:00    | 15:10:00      | 16:10:00    | 18:10:00      | 18:45:00    | 19:10:00      | 20:10:00    |
    |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
    
  2. 规范化版本:

    dateperiode 表格

    | period_id | date_from  | date_to    |
    |-----------|------------|------------|
    | 1         | 10/09/2015 | 11/09/2015 |
    | 2         | 12/09/2015 | 13/09/2015 |
    |-------------------------------------|
    

    sessionrange 表格

    | sessionrange_id | session   | session_from | session_to  |
    |-----------------|-----------|--------------|-------------|
    | 1               | 1         | 04:00:00     | 05:00:00    |
    | 2               | 2         | 12:00:00     | 13:00:00    |
    | 3               | 3         | 15:00:00     | 16:00:00    |
    | 4               | 4         | 18:00:00     | 18:35:00    |
    | 5               | 5         | 19:00:00     | 20:00:00    |
    | 6               | 1         | 04:10:00     | 05:10:00    |
    | 7               | 2         | 12:10:00     | 13:10:00    |
    | 8               | 3         | 15:10:00     | 16:10:00    |
    | 9               | 4         | 18:10:00     | 18:45:00    |
    | 10              | 5         | 19:10:00     | 20:10:00    |
    |-----------------|-----------|--------------|-------------|
    

    timsetup 表格

    | period_id | sessionrange_id |
    |-----------|-----------------|
    | 1         | 1               |
    | 1         | 2               |
    | 1         | 3               |
    | 1         | 4               |
    | 1         | 5               |
    | 2         | 6               |
    | 2         | 7               |
    | 2         | 8               |
    | 2         | 9               |
    | 2         | 10              |
    |-----------|-----------------|
    
    1. checktime,此表包含指纹扫描程序(tapping_time字段)的数据

      |userid  | tapping_time         |
      |--------|----------------------|
      |234     | 10/09/2015 04:20:04  |
      |234     | 10/09/2015 04:20:06  |
      |234     | 10/09/2015 12:15:35  |
      |234     | 10/09/2015 15:31:11  |
      |234     | 10/09/2015 18:19:10  |
      |234     | 10/09/2015 18:19:15  |
      |234     | 10/09/2015 19:37:53  |
      |234     | 11/09/2015 04:38:42  |
      |234     | 11/09/2015 04:38:47  |
      |234     | 11/09/2015 12:21:27  |
      |234     | 11/09/2015 15:45:30  |
      |234     | 11/09/2015 15:45:37  |
      |234     | 11/09/2015 18:27:15  |
      |234     | 11/09/2015 19:55:08  |
      |234     | 11/09/2015 19:55:12  |
      |234     | 12/09/2015 04:45:10  |
      |234     | 12/09/2015 04:45:13  |
      |234     | 12/09/2015 13:12:55  |
      |234     | 12/09/2015 16:35:08  |
      |234     | 12/09/2015 18:49:10  |
      |234     | 12/09/2015 20:20:57  |
      |234     | 13/09/2015 05:11:56  |
      |234     | 13/09/2015 05:12:05  |
      |234     | 13/09/2015 12:45:13  |
      |234     | 13/09/2015 15:47:25  |
      |234     | 13/09/2015 18:31:27  |
      |234     | 13/09/2015 18:31:30  |
      |234     | 13/09/2015 20:01:18  |
      |-------------------------------|
      
    2. <code>checktime</code> table

      我尝试创建一个Access查询,将日期/时间分组到timesetup表中的句点。 tapping_time基于timesetup表中的会话条件,每个会话应该只有一个结果(即使用户一次点击多次)。此外,用户点击会话外会话(session_fromsession_to之间的时间)字段从不计算或显示在结果表中。

      期望的结果:

      | userid | date       | tapping_on | session |
      |--------|------------|------------|---------|
      | 234    | 10/09/2015 | 04:20:04   | 1       |
      | 234    | 10/09/2015 | 12:15:35   | 2       |
      | 234    | 10/09/2015 | 15:31:11   | 3       |
      | 234    | 10/09/2015 | 18:19:10   | 4       |
      | 234    | 10/09/2015 | 19:37:53   | 5       |
      | 234    | 11/09/2015 | 04:38:42   | 1       |
      | 234    | 11/09/2015 | 12:21:27   | 2       |
      | 234    | 11/09/2015 | 15:45:30   | 3       |
      | 234    | 11/09/2015 | 18:27:15   | 4       |
      | 234    | 11/09/2015 | 19:55:08   | 5       |
      | 234    | 12/09/2015 | 04:45:10   | 1       |
      | 234    | 12/09/2015 | 16:35:08   | 3       |
      | 234    | 12/09/2015 | 18:49:10   | 4       |
      | 234    | 13/09/2015 | 12:45:13   | 2       |
      | 234    | 13/09/2015 | 15:47:25   | 3       |
      | 234    | 13/09/2015 | 18:31:27   | 4       |
      | 234    | 13/09/2015 | 20:01:18   | 5       |
      |--------------------------------------------|
      

1 个答案:

答案 0 :(得分:0)

你可能在规范化方面有点过分,因为dateperiode和sessionrange之间的关系看起来像是一对多的关系,因此不需要联结表。

通过以下数据,我相信以下查询应该可以正常运行,但不幸的是,我没有在准备好的情况下进行不错的测试设置:

SELECT userid, tDatepart As [date], Min(t.tapping_time) As tapping_on, session FROM 
    (
        SELECT session_from, session_to, date_from, date_to, session
        FROM timsetup i
        INNER JOIN dateperiode d ON i.period_id = d.period_id
        INNER JOIN sessionrange q ON t.sessionrange_id = q.sessionrange_id
    ) As s
INNER JOIN 
    (SELECT Int(tapping_time) As tDatepart, CDate(tapping_time - Int(tapping_time)) As tTimepart, userid, tapping_time
    FROM checktime) t
ON (t.tDatePart >= s.date_from AND t.tDatePart <= s.date_to AND t.tTimePart >= s.session_from AND t.tTimePart <= s.session_to)
GROUP BY userid, tDatepart, session