Results 1 to 5 of 5
  1. #1
    Mattbro is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Mar 2012
    Posts
    95

    Counting no of records from a query expression field within a date range

    Hi All,
    Working on an audit database, I have a table 'Sheet2' with the columns [Receive Date], [Case No] (PK), and [Date Report Updated]. I have set a form for the user to enter the 'Start' and 'End' parameters on [Receive Date], and have an expression in the query to calculate the Total time of the Case- TotalTime: DateDiff('d',[Receive Date],[Date Report Updated]).

    What I am failing miserably in doing is trying to develop expressions to calculate;


    a count of the number of Case No's in which the TotalTime is <5 days
    a count of the number of Case No's in which the TotalTime is <7 days
    ....within the duration specified by the 'Start' and 'End' parameters of the receive date.
    Trying all sorts but no joy. Basic SQL reads;

    Code:
    SELECT Sheet2.[Receive Date], Sheet2.[Case No], Sheet2.[Date Report Updated], Sheet2.[Date Report Status1 Achieved], DateDiff('d',[Receive Date],[Date Report Updated]) AS TotalTime
    FROM Sheet2
    WHERE (((Sheet2.[Receive Date]) Between [Forms]![Audit]![Start] And [Forms]![Audit]![End]));
    Is this possible? Any info gratefully received.
    Mattbro

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Use that query as the source for another:

    SELECT Sum(IIf(TotalTime<5,1,0) AS CountUnder5, Sum(IIf(TotalTime<7,1,0) AS CountUnder7 FROM query1;

    The result will be a single record with two values.

    An alternative is to use DCount() domain aggregate function in the first query or possibly nesting the above aggregate query as a subquery, both of which will show the same data on every row of the query.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Mattbro is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Mar 2012
    Posts
    95
    Thanks June7, but I am getting an error message- 'Syntax Error (Missing operator) in query expression;SELECT Sum(IIf(TotalTime<5,1,0) AS CountUnder5, Sum(IIf(TotalTime<7,1,0) AS CountUnder7 FROM Audit;'
    I am using Access 2003-would that cause it?
    I have some success with SELECT Count(Audit.TotalTime) AS CountOfTotalTime FROM Audit HAVING ((Audit.TotalTime)<5);
    But ultimately what I want to do is achieve a count for those<5, count for those<7 and express them as a percentage against the total number of cases within that timeframe......in a report. I'll keep at it, but if you can see what the syntax error is, I would be very grateful.
    Regards,
    Mattbro

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Sorry, I missed some parens, try:

    SELECT Sum(IIf(TotalTime<5,1,0)) AS CountUnder5, Sum(IIf(TotalTime<7,1,0)) AS CountUnder7 FROM query1;

    Remember, parens (as well as " and ' marks) have to be in pairs.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Mattbro is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Mar 2012
    Posts
    95
    Done! Excellent-works a treat. Thanks June7!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 07-18-2013, 04:21 AM
  2. Replies: 5
    Last Post: 07-05-2013, 11:16 AM
  3. Replies: 10
    Last Post: 04-17-2013, 04:15 PM
  4. Replies: 17
    Last Post: 01-16-2013, 08:22 PM
  5. Multiple Field & date range filter
    By mrkandohi001 in forum Reports
    Replies: 6
    Last Post: 01-18-2012, 03:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums