开发者

Access 2003: How to combine two count queries

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.

PAR开发者_运维技巧AMETERS 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.


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.


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


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 #];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜