Results 1 to 8 of 8
  1. #1
    Shawnf is offline Novice
    Windows 10 Access 2007
    Join Date
    May 2016
    Posts
    5

    Counting with Criteria


    I have a table like this
    PN COUNT DATE
    ABC 1/18/2016
    ABC 1/20/2016
    ABC 5/5/2016
    XYZ 1/12/2016
    XYZ 1/18/2016
    XYZ 5/10/2016
    HIJ 1/8/2016
    HIJ 1/12/2016

    All PNs have to be counted 4 times a year
    the issue I have is there is a criteria that if a PN is counted within 62 days of each count that it does not count as an actual count.

    I need to have this information sorted to one of these two options and I cannot figure it out. Notice that each of the second count dates from above are not included in the output as there was not 62 days that have lapsed since the first count
    PN First Count Second Count Third Count Forth Count
    ABC 1/18/2016 5/5/2016
    XYZ 1/12/2016 5/10/2016
    HIJ 1/8/2016

    OR
    PN Times Counted
    ABC 2
    XYZ 2
    HIJ 1

    Any help here would be greatly appreciated.

    thank you

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    The second is pretty easy with a totals query:

    SELECT PN, Count(*) AS TimesCounted
    FROM TableName
    GROUP BY PN
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Shawnf is offline Novice
    Windows 10 Access 2007
    Join Date
    May 2016
    Posts
    5
    That gives me a total of how many times it has been counted. the issue is with the 62 day requirement it isn't the total number of times counted.
    in the example above the 2nd count for each PN does not count as a count. so just a Count() operation will not work.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Ah, sorry, I didn't read well enough. I wonder if you can get the previous record on the same line:

    http://allenbrowne.com/subquery-01.html#AnotherRecord

    do the math, and add a criteria to exclude the appropriate records. Might take two queries.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Shawnf is offline Novice
    Windows 10 Access 2007
    Join Date
    May 2016
    Posts
    5
    This may be headed in the right direction. Thank you. I am still very new to access, and don't fully understand SQL, so please forgive me for sounding totally newb. Any further help with the actual coding would be very helpful.

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928

  7. #7
    Shawnf is offline Novice
    Windows 10 Access 2007
    Join Date
    May 2016
    Posts
    5
    Apparently it is not right to post the same issue on multiple forums without linking to the other, so here it is...
    http://www.access-programmers.co.uk/...d.php?t=287076

    same issue different forum.

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,928
    try

    Code:
    SELECT PN, Count(*) AS TimesCounted
    FROM pnTable
    WHERE not exists(SELECT * FROM pnTable T WHERE CountDate between pnTable.CountDate and pnTable.CountDate-62)
    GROUP BY PN

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

Similar Threads

  1. Replies: 7
    Last Post: 12-12-2014, 11:58 AM
  2. Replies: 8
    Last Post: 10-22-2013, 05:08 PM
  3. Counting with multiple criteria in a field
    By pfunk in forum Queries
    Replies: 10
    Last Post: 08-13-2013, 11:45 AM
  4. Counting text entries based on criteria
    By benjammin in forum Queries
    Replies: 6
    Last Post: 07-22-2013, 09:55 AM
  5. Counting records based on specified criteria
    By GrnISSO in forum Queries
    Replies: 2
    Last Post: 10-04-2007, 03:07 PM

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