views:

270

answers:

1

I have a table containing reports and the date/time they were created. I'd like to create a graph to tally the number of reports created during intervals of ten minutes between two time periods: 8:00AM-9:00AM and 1:00PM-2:00PM.

Here's an example of a query I'd like to run:

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"

Unfortunately, it seems that Access isn't able to tell that the field I'd like to format within the FORMAT function refers to the field of the same name in the query. In the above case, I assume this is because Access is treating this as a string literal. However, this doesn't work, either:

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 is prompting me for the value for 'first'.

I feel like I'm going about this the wrong way to begin with. Can anyone point me in the right direction?

More info: I'm using the Jet engine through ODBC, but the data is actually stored in SQL Server 2005, so if necessary I suppose I could write a pass-through query. Ideally, however, I'd like it to work in Access.

EDIT:

To be clear, the following DOES work:

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";

This, however, doesn't allow me to select the same field multiple times under multiple conditions.

The first example at http://office.microsoft.com/en-us/access/HA012288391033.aspx#examples suggests that wrapping the alias of the field in square braces is the correct way to reference a record source, but the following still results in a prompt for the value of 'first':

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";

Edit II:

An example of the contents of StudyStartDateTime of a few records:

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 sees this field as a "Date/Time" datatype.

My end goal is to graph the tally for each interval. I think the easiest way of doing this would be to simply select each date/time as a different alias. For instance:

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

The problem you are having is because access doesn't reference fields by their aliases within the same query. Meaning you can't assign an alias in the select clause and use it in the where clause.

As for the first part of your question, you can try something like this

    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

This will give you a tally for each 10 minute period.

Jeremy
Perfect. I knew I was going about this the wrong way. Thanks!
Richard