在Access中绘制时间间隔

时间:2009-09-03 14:58:38

标签: sql sql-server database ms-access ms-access-2007

我的表格如下所示:

StudyId  StudyStartDateTime
============================
1        01/01/2009 08:45 AM
2        01/01/2009 08:53 AM
...

我想返回一个包含StudyId的查询,以及启动它的十分钟间隔:

StudyId   Interval
==================
1         8:40 AM
2         8:50 AM

这是因为我想创建一个图表,显示十分钟间隔内的研究分布。

间隔时间是上午8点到9点之间,下午1点到下午2点。我已经走到了这一步:

SELECT s.studyid,
       s.studystartdatetime,
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:00:00 AM" AND "08:09:59 AM",
           "8:00 - 8:10",0) AS ["8:00 - 8:10"],
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:10:00 AM" AND "08:19:59 AM",
           "8:10 - 8:20",0) AS ["8:10 - 8:20"],
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:20:00 AM" AND "08:29:59 AM",
           "8:20 - 8:30",0) AS ["8:20 - 8:30"],
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:30:00 AM" AND "08:39:59 AM",
           "8:30 - 8:40",0) AS ["8:30 - 8:40"],
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:40:00 AM" AND "08:49:59 AM",
           "8:40 - 8:50",0) AS ["8:40 - 8:50"],
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "08:50:00 AM" AND "08:59:59 AM",
           "8:50 - 9:00",0) AS ["8:50 - 9:00"],
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:00:00 PM" AND "01:09:59 PM",
           "1:00 - 1:10",0) AS ["1:00 - 1:10"],
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:10:00 PM" AND "01:19:59 PM",
           "1:10 - 1:20",0) AS ["1:10 - 1:20"],
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:20:00 PM" AND "01:29:59 PM",
           "1:20 - 1:30",0) AS ["1:20 - 1:30"],
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:30:00 PM" AND "01:39:59 PM",
           "1:30 - 1:40",0) AS ["1:30 - 1:40"],
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:40:00 PM" AND "01:49:59 PM",
           "1:40 - 1:50",0) AS ["1:40 - 1:50"],
       Iif(Format(s.studystartdatetime,'hh:nn:ss AM/PM') BETWEEN "01:50:00 PM" AND "01:59:59 PM",
           "1:50 - 2:00",0) AS ["1:50 - 2:00"]
FROM   dbo_study_viewx211_rpt AS s

结果是这样的:

StudyId   StudyStartDateTime    8:00 - 8:10   8:10 - 8:20  ...
==================================================================
1         28/11/2007 8:15:22 AM           0   8:10 - 8:20    0   ...
...

如何将所有间隔分组到一个“间隔”列中?

2 个答案:

答案 0 :(得分:1)

我认为让查询输出StudyStartDateTime

会更容易
SELECT s.studyid,       
       s.studystartdatetime,
FROM   dbo_study_viewx211_rpt AS s

然后切换到“数据透视表”视图,并使用数据透视表的分组功能进行格式化和分组。

答案 1 :(得分:1)

通过使用istart和iend作为日期/时间数据类型创建TimeIntervals表来查看是否更容易执行所需操作。

istart     iend       ilabel
8:00:00 AM 8:09:59 AM 8:00-8:10
8:10:00 AM 8:19:59 AM 8:10-8:20
8:20:00 AM 8:29:59 AM 8:20-8:30
8:30:00 AM 8:39:59 AM 8:30-8:40
8:40:00 AM 8:49:59 AM 8:40-8:50
8:50:00 AM 8:59:59 AM 8:50-9:00
9:00:00 AM 9:09:59 AM 9:00-9:10

然后尝试这样的查询:

SELECT
    s.StudyId
    , s.StudyStartDateTime
    , t.istart
    , t.iend
    , t.ilabel
FROM
    dbo_study_viewx211_rpt AS s
    , TimeIntervals AS t
WHERE
    TimeValue(s.StudyStartDateTime) Between t.istart And t.iend;

查询的示例输出:

StudyId StudyStartDateTime  istart     iend       ilabel
1       1/1/2009 8:45:00 AM 8:40:00 AM 8:49:59 AM 8:40-8:50
2       1/1/2009 8:53:00 AM 8:50:00 AM 8:59:59 AM 8:50-9:00

您可能不希望查询结果中显示StudyStartDate,istart和iend字段。我把它们留在这里,希望能让它更容易理解它是如何工作的。