Results 1 to 3 of 3
  1. #1
    Antonio is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    37

    Selecting grouped records where certain criteria is met


    Hi there,


    I am attempting something but am stuck mainly at the point of selecting the records where they have the same date.

    I have uploaded the dataset I am working on to make sure there are no misunderstandings and to make explanation easier.

    I am trying to select records within the 'Attribute' dataset where the same Constituent ID has an:

    (Constituent...Description = 'Email opt in' AND Constituent...Description = 'Email opt out' AND both Constituent...Descriptions have the SAME date) OR
    (Constituent...Description = 'Postal opt in' AND Constituent...Description = 'Postal opt out' AND both Constituent...Descriptions have the SAME date) OR
    (Constituent...Description = 'Telephone opt in' AND Constituent...Description = 'Telephone opt out' AND both Constituent...Descriptions have the SAME date) OR
    (Constituent...Description = 'SMS opt in' AND Constituent...Description = 'SMS opt out' AND both Constituent...Descriptions have the SAME date) OR

    I want to export all the Constituent IDs from these selected records.

    I dont know if any records exist that meet this criteria so dont have any examples, but thats what I am trying to find out!

    A fake example (i.e. this does not occur in the dataset, but just to demonstrate the principle):


    Constituent ID Constituent Specific Attributes Contact Preferences Description Constituent Specific Attributes Contact Preferences Date
    1 Email opt in 01/01/2000
    1 Email opt out 01/01/2000
    1 Telephone opt in
    1 SMS opt out 01/06/2015
    2 Telephone opt out 01/10/2017
    2 Telephone opt in 01/10/2017


    Again, any help will be much appreciated.


    Antonio
    Attached Files Attached Files

  2. #2
    isladogs's Avatar
    isladogs is offline Access MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    6,204
    The way this is done is to add the attribute table to the query designer twice joining by ID and Date fields

    Click image for larger version. 

Name:	Capture.PNG 
Views:	6 
Size:	22.2 KB 
ID:	34021

    Code:
    SELECT Attribute.[Constituent ID], Attribute.[Constituent Specific Attributes Contact Preferences Description], Attribute_1.[Constituent Specific Attributes Contact Preferences Description], Attribute.[Constituent Specific Attributes Contact Preferences Date]FROM Attribute INNER JOIN Attribute AS Attribute_1 ON (Attribute.[Constituent Specific Attributes Contact Preferences Date] = Attribute_1.[Constituent Specific Attributes Contact Preferences Date]) AND (Attribute.[Constituent ID] = Attribute_1.[Constituent ID])
    WHERE (((Attribute.[Constituent Specific Attributes Contact Preferences Description])="EMail opt in") AND ((Attribute_1.[Constituent Specific Attributes Contact Preferences Description])="Email opt out") AND ((Attribute.[Constituent Specific Attributes Contact Preferences Date]) Is Not Null)) OR (((Attribute.[Constituent Specific Attributes Contact Preferences Description])="Postal opt in") AND ((Attribute_1.[Constituent Specific Attributes Contact Preferences Description])="Postal opt out") AND ((Attribute.[Constituent Specific Attributes Contact Preferences Date]) Is Not Null)) OR (((Attribute.[Constituent Specific Attributes Contact Preferences Description])="Telephone opt in") AND ((Attribute_1.[Constituent Specific Attributes Contact Preferences Description])="Telephone opt out") AND ((Attribute.[Constituent Specific Attributes Contact Preferences Date]) Is Not Null)) OR (((Attribute.[Constituent Specific Attributes Contact Preferences Description])="SMS opt in") AND ((Attribute_1.[Constituent Specific Attributes Contact Preferences Description])="SMS opt out") AND ((Attribute.[Constituent Specific Attributes Contact Preferences Date]) Is Not Null));
    You'll be pleased to know there are no results for this query
    Colin Riddington, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I know I don't know, I keep quiet!

  3. #3
    Antonio is offline Advanced Beginner
    Windows 10 Access 2010 64bit
    Join Date
    Apr 2018
    Posts
    37
    Ah yes, that works perfectly!

    Again, another VERY useful principle to learn which I will adapt in future use for similar situations. Before doing this exercise, I had always selected records using fairly basic SQL BETWEEN datasets; this is the 1st exercise where I have had to select data based upon a SINGLE dataset - you get around this problem by effectively 'duplicating' the dataset to allow comparisons between the same fields.

    Again, thanks very much Colin, really appreciated.

    At this rate I might actually be in a position to answer other peoples questions soon


    Antonio

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

Similar Threads

  1. Replies: 6
    Last Post: 12-10-2015, 02:39 PM
  2. Replies: 2
    Last Post: 05-06-2015, 02:11 PM
  3. Selecting records with matching criteria
    By Daryl2106 in forum Access
    Replies: 2
    Last Post: 09-07-2013, 03:57 AM
  4. Replies: 5
    Last Post: 08-02-2012, 09:44 AM
  5. Selecting records based on criteria from user input
    By shabbaranks in forum Programming
    Replies: 1
    Last Post: 01-04-2012, 09:06 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