tags:

views:

51

answers:

3

I am writing my first little Access 2003 application. I have two queries that I cannot figure out how to combine. What I am looking for is to get all the routes numbers in a date range, count the total rows per route in the date range, and count how many were late. I have these two queries that get me part of the way there, just can't figure out how to combine them in an Access query.

PARAMETERS StartDate DateTime, EndDate DateTime;
SELECT [ROUTE #], Count(*) AS Total
FROM [On time performace audits] 
WHERE ((([On time performace audits].DATE) Between [StartDate] And [EndDate]))
GROUP BY [Route #];

PARAMETERS StartDate DateTime, EndDate DateTime;
SELECT [Route #], Count(*) as Late
FROM [On time performace audits]
WHERE ([On time performace audits].DATE Between [StartDate] And [EndDate]) AND (    Minute( [ACTUAL TIME]- [SCHEDULED TIME]) >=6)
GROUP BY [Route #];

I'm not much of a SQL guy, more of a LINQ guy, and writing a quick and dirty app in Access 2003 after being used to VS2010 is a bit painful. Anyways, hope you can help. Thanks.

A: 

In your second example query, are you certain you want the Minute() function?

If you want a duration between 2 date/time values, use the DateDiff() function.

Debug.Print DateDiff("n", #2010/09/27 01:00#, #2010/09/27 02:10#)
70

Debug.Print Minute(#2010/09/27 02:10# - #2010/09/27 01:00#)
10

If the DateDiff() approach is what you want, try this:

PARAMETERS StartDate DateTime, EndDate DateTime;
SELECT t.[ROUTE #], t.Total, l.Late
FROM
    (SELECT [ROUTE #], Count(*) AS Total
    FROM [On time performace audits]
    WHERE ((([DATE]) Between [StartDate] And [EndDate]))
    GROUP BY [ROUTE #]) AS t
    LEFT JOIN (
        SELECT [ROUTE #], Count(*) AS Late
        FROM [On time performace audits]
        WHERE ((([DATE]) Between [StartDate] And [EndDate]))
            And DateDiff("n",[SCHEDULED TIME],[ACTUAL TIME]) >= 6
        GROUP BY [ROUTE #]
        ) AS l
    ON t.[ROUTE #] = l.[ROUTE #];

Note I enclosed the DATE field name with square brackets to distinguish it from the VBA Date() function.

HansUp
You're the man! Thank you. I had something close to this once, but was doing something wrong 'cause it wasn't working. And thanks for adding DateDiff(), I suspected as much with Minute().
Deke
A: 

You could also use a UNION [ALL] query to pull results sets together into one...

TonBill
A: 

the question is how to combine the two queries. UNION ALL would be the best way IMO, like this:

PARAMETERS StartDate DateTime, EndDate DateTime; SELECT 'All' As Type, [ROUTE #], Count(*) AS Total FROM [On time performace audits]
WHERE DATE Between StartDate And EndDate GROUP BY [Route #] UNION ALL SELECT 'Late', [Route #], Count(*) FROM [On time performace audits] WHERE DATE Between StartDate And [EndDate AND (Minute( [ACTUAL TIME]- [SCHEDULED TIME]) >=6) GROUP BY [Route #];

Carlos