从Access中的日期时间字段中选择时间范围

时间:2009-09-02 16:17:33

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

我有一张包含报告及其创建日期/时间的表格。我想创建一个图表来计算两个时间段之间的十分钟间隔创建的报告数量:上午8:00到9:00和下午1:00到下午2:00。

以下是我想要运行的查询示例:

SELECT
s.StudyStartDateTime AS "8:00 - 8:10",
s.StudyStartDateTime AS "8:10 - 8:20",
s.StudyStartDateTime AS "8:20 - 8:30",
s.StudyStartDateTime AS "8:30 - 8:40",
s.StudyStartDateTime AS "8:40 - 8:50",
s.StudyStartDateTime AS "8:50 - 9:00",
s.StudyStartDateTime AS "1:00 - 1:10",
s.StudyStartDateTime AS "1:10 - 1:20",
s.StudyStartDateTime AS "1:20 - 1:30",
s.StudyStartDateTime AS "1:30 - 1:40",
s.StudyStartDateTime AS "1:40 - 1:50",
s.StudyStartDateTime AS "1:50 - 2:00"
FROM
    dbo_Study_ViewX211_Rpt AS s
WHERE FORMAT("8:00 - 8:10", 'hh:mm:ss') BETWEEN "08:00:00" AND "08:09:59"
AND FORMAT("8:10 - 8:20", 'hh:mm:ss') BETWEEN "08:10:00" AND "08:19:59"
AND FORMAT("8:20 - 8:30", 'hh:mm:ss') BETWEEN "08:20:00" AND "08:29:59"
AND FORMAT("8:30 - 8:40", 'hh:mm:ss') BETWEEN "08:30:00" AND "08:39:59"
AND FORMAT("8:40 - 8:50", 'hh:mm:ss') BETWEEN "08:40:00" AND "08:49:59"
AND FORMAT("8:50 - 9:00", 'hh:mm:ss') BETWEEN "08:50:00" AND "08:59:59"
AND FORMAT("1:00 - 1:10", 'hh:mm:ss') BETWEEN "01:00:00" AND "01:09:59"
AND FORMAT("1:10 - 1:20", 'hh:mm:ss') BETWEEN "01:10:00" AND "01:19:59"
AND FORMAT("1:20 - 1:30", 'hh:mm:ss') BETWEEN "01:20:00" AND "01:29:59"
AND FORMAT("1:30 - 1:40", 'hh:mm:ss') BETWEEN "01:30:00" AND "01:39:59"
AND FORMAT("1:40 - 1:50", 'hh:mm:ss') BETWEEN "01:40:00" AND "01:49:59"
AND FORMAT("1:50 - 2:00", 'hh:mm:ss') BETWEEN "01:50:00" AND "01:59:59"

不幸的是,似乎Access无法判断我想在FORMAT函数中格式化的字段是否指向查询中的同名字段。在上面的例子中,我假设这是因为Access将其视为字符串文字。但是,这不起作用:

SELECT
    s.StudyStartDateTime AS first
FROM
    dbo_Study_ViewX211_Rpt AS s
WHERE FORMAT(first, 'hh:mm:ss') BETWEEN "08:00:00" AND "08:09:59"

Access正在提示我输入'first'的值。

我觉得我开始时这是错误的方式。有人能指出我正确的方向吗?

更多信息:我通过ODBC使用Jet引擎,但数据实际存储在SQL Server 2005中,因此如果有必要,我想我可以编写传递查询。但是,理想情况下,我希望它能在Access中使用。

编辑:

要明确的是,以下内容有效:

SELECT s.StudyStartDateTime AS first
FROM dbo_Study_ViewX211_Rpt AS s
WHERE FORMAT(StudyStartDateTime,'hh:mm:ss') Between "08:00:00" And "08:09:59";

然而,这不允许我在多个条件下多次选择相同的字段。

http://office.microsoft.com/en-us/access/HA012288391033.aspx#examples的第一个例子表明,用方括号包装字段的别名是引用记录源的正确方法,但是以下内容仍然会提示“first”的值:< / p>

SELECT s.StudyStartDateTime AS first
FROM dbo_Study_ViewX211_Rpt AS s
WHERE FORMAT([first],'hh:mm:ss') Between "08:00:00" And "08:09:59";

编辑II:

一些记录的StudyStartDateTime内容示例:

29/11/2007 8:06:00 AM
30/11/2007 8:09:01 AM
05/12/2007 8:06:51 AM
06/12/2007 8:07:21 AM

Access将此字段视为“日期/时间”数据类型。

我的最终目标是绘制每个间隔的计数。我认为最简单的方法是简单地选择每个日期/时间作为不同的别名。例如:

8:00-8:10   8:10-8:20   8:20:8:30   ...
=======================================
8:01        
8:03
            8:15
                        8:23
                        8:28
                        8:28
...

1 个答案:

答案 0 :(得分:3)

您遇到的问题是因为访问不会在同一查询中通过别名引用字段。这意味着您无法在select子句中分配别名并在where子句中使用它。

至于你问题的第一部分,你可以尝试这样的事情

    SELECT 
        sum(iif(FORMAT(s.StudyStartDateTime,'hh:mm:ss') 
                BETWEEN "08:00:00" AND "08:09:59",1,0)) as "8:00 - 8:10",
        sum(iif(FORMAT(s.StudyStartDateTime,'hh:mm:ss') 
                BETWEEN "08:10:00" AND "08:19:59",1,0)) as "8:10 - 8:20",
        ...
    FROM dbo_Study_ViewX211_Rpt AS s

这将为您提供每10分钟一次的计数。