Results 1 to 5 of 5
  1. #1
    jakeman is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Location
    Connecticut
    Posts
    8

    Create a query to filter out records

    Hey everyone - I'm a beginner level Access user and I'm having trouble writing a query. I have a very simple table called CustomerActvity with 5 attributes: CustID, CustName, MembershipType, DatePurchased, and MembershipAmt.



    If a customer has purchased more than one type of membership, I'm trying to show those customers in my query for targeted emails. So basically when Count(CustID) > 1 in my table, show those Customers in my results, with all attributes returned.

    Is this best handled using a sub-query, maybe?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    make Q1 to count the customers in the date range.

    then Q2 to pull the count>1
    Q2:
    select CustID from Q1 where [CountOfPurchases]>1

  3. #3
    jakeman is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Oct 2011
    Location
    Connecticut
    Posts
    8
    Thanks, ranman. If I were to do this all in the same query, how would it look?

  4. #4
    JoeM is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    You would probably need to write the SQL code directly (instead of using the Query Builder), and it would look something like this:
    Code:
    SELECT *
    FROM CustomerActivity
    WHERE CustID In
    (SELECT CustomerActivity.CustID
    FROM CustomerActivity
    GROUP BY CustomerActivity.CustID
    HAVING Count(CustomerActivity.CustID)>1);

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Build another query that JOINS the aggregate query to the table. Then switch to SQLView and copy/paste the SQL of aggregate query into the second query within parens (be sure to delete the semi-colon from the inner SQL statement).

    SELECT CustomerActvity.*, Q1.* FROM CustomerActvity INNER JOIN (SELECT …) AS Q1 ON Q1.CustID = CustomerActvity.CustID WHERE CountOfPurchases >1;

    Filtering on the count parameter can be done in the aggregate query or the outer query.

    EDIT: Just saw JoeM reply - yes, IN operator is a good alternative. Can still use copy/paste to build the final query SQL, then delete the query object no longer needed.


    Do you have Customers table? Really should not have both CustID and CustName in CustomerActivity - just the CustID. Then retrieve CustName from Customers in queries that join tables.
    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.

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

Similar Threads

  1. Want to Create a Custom Table/Query Filter
    By workguy in forum Access
    Replies: 5
    Last Post: 02-05-2018, 09:36 AM
  2. Replies: 3
    Last Post: 08-27-2015, 09:01 PM
  3. Replies: 3
    Last Post: 11-27-2012, 07:20 AM
  4. Filter Out Old Records in a Query
    By SMC in forum Queries
    Replies: 5
    Last Post: 06-19-2012, 10:21 AM
  5. Replies: 3
    Last Post: 07-10-2011, 05:37 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