Results 1 to 4 of 4
  1. #1
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368

    distinct count with criteria and max date

    Hi all,

    I'm trying to get this in one go but the SQL just isn't coming to me. We have a program that customers can enroll in. The table stores, among other things, account numbers, application status, and application dates. Everytime something happens in relation to an application, a new record is generated.

    What I need to pull is the count of account numbers in which the most recent record of a given month has a specific application status.



    What I'm going with so far:

    Code:
    SELECT
    Count(AcctNum) As CountOfAccts
    FROM
    (SELECT AcctNum, Max(ApplicationDate) AS MaxAppDate
    FROM tblApplications
    WHERE ApplicationStat = 1
    AND ApplicationDate BETWEEN #7/1/2011# AND #7/31/2011#
    GROUP BY AcctNum
    )
    correct? incorrect? better way?

    EDIT: The reason I'm not just brute forcing my way is that I'll be hitting a production mainframe (yes, a mainframe) with an apprx 20 min query. DBAs aren't too thrilled with the idea anyway. I'll be skinned alive if they find me testing out my SQL =P

    EDIT2: Added GROUP BY to nested query

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    I think you need GROUP BY clause in the inner nested 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
    TheShabz is offline Court Jester
    Windows XP Access 2003
    Join Date
    Feb 2010
    Posts
    1,368
    Correct, I do. I had it here but missed it during transcription. Edited OP

  4. #4
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229
    Don't you also need an alias for the inner query?

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

Similar Threads

  1. Distinct count in a query
    By Fabdav in forum Access
    Replies: 5
    Last Post: 09-14-2011, 04:53 PM
  2. Replies: 1
    Last Post: 02-25-2011, 06:11 PM
  3. Count Distinct in Access
    By georgerudy in forum Access
    Replies: 1
    Last Post: 11-28-2010, 01:24 PM
  4. Access DB distinct count
    By anziga in forum Queries
    Replies: 3
    Last Post: 10-12-2010, 02:20 PM
  5. Replies: 0
    Last Post: 08-08-2008, 08:34 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