Results 1 to 5 of 5
  1. #1
    pinecrest515 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Posts
    31

    Help with Query!!! (Interesting Problem)

    Dear Forumers,

    I figured out that my previous MS Access SQL statement, which I thought solved a problem, doesn't:

    Suppose I have the following table:

    First_Name Last_Name Age_Group Vaccinated Age
    John Smith Adult Yes 30
    Mary Smith Child Yes 5
    Casey Smith Child No 12
    Joe Smith Child No 6


    Sam Walton Adult Yes 31
    Thomas Walton Child No 15
    John Walton Child Yes 5
    Mary Scott Adult Yes 56
    Andrew Johnson Adult Yes 55
    Bryon Johnson Child No 12


    I am trying to query for a list of Adults that have all their children (who have same last name) either Vaccinated or over the age of 10.

    Ideally, the query should output the following:
    First_Name Last_Name Age_Group Vaccinated Age
    Sam Walton Adult Yes 31
    Andrew Johnson Adult Yes 55


    Sam Walton is returned, because he has 1 child over 15 and 1 child vaccinated.
    Andrew Johnson is returned, because although his child is not vaccinated, the child is over 10 years old.
    John Smith is not returned, because although 2 of his children are either vaccinated or over 10, the third child is not vaccinated and under 10.
    Mary Scott is not returned, because she has no children.


    Currently, my query finds a list of Adults that have at least one child vaccinated or one child over 10. If an adult has one child under 10 who is not vaccinated, but has another child over 10 or vaccinated, then the query will still return the adult's record. This is not robust - I would like only adults who have all their children either vaccinated or over 10.


    My current, un-robust query:
    SELECT DISTINCT
    A.*
    FROM
    [Table1] AS [A]
    INNER JOIN
    [Table1] AS [B]
    ON [A].[Last_Name] = [B].[Last_Name]
    WHERE
    [A].[Age_Group] = 'Adult'
    AND [B].[Age_Group] = 'Child'
    AND
    ([B].[Vaccinated] = 'Yes' OR [B].[Age] > 10 )

    Currently, my un-robust query is outputting the following:
    First_Name Last_Name Age_Group Vaccinated Age
    Sam Walton Adult Yes 31
    John Smith Adult Yes 30
    Andrew Johnson Adult Yes 55



    How should I best construct this query to achieve the desired results?

    Thanks in advance for your reply!

  2. #2
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    I think I gave reply for your similar post yesterday, didn't I?

  3. #3
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    first step, find out the last names under 10 and not vaccinated.
    second, exclude these last names.

    Please refer to yesterday's post for more details.

  4. #4
    pinecrest515 is offline Advanced Beginner
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Posts
    31
    Thanks weekend! Your suggestion helps the sample query to a good extent! Appreciate it! However, I am still running into some additional complexities with my actual query.

  5. #5
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    two step in one query:

    select table1.* from table1 left join
    (select distinct last_name from table1 where age<=10 and vaccinated="No")
    as b on table1.last_name=b.last_name where b.last_name is null and age_group="Adult"

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

Similar Threads

  1. VBA problem in query
    By geniass in forum Queries
    Replies: 41
    Last Post: 08-28-2010, 03:07 PM
  2. Interesting Query Problem
    By Lockrin in forum Queries
    Replies: 7
    Last Post: 08-23-2010, 01:56 PM
  3. Interesting Issue With Report
    By dynamictiger in forum Reports
    Replies: 1
    Last Post: 04-15-2010, 05:40 AM
  4. query problem i have a problem wi
    By maxx3 in forum Queries
    Replies: 0
    Last Post: 06-29-2009, 02:29 AM
  5. query problem
    By maxx3 in forum Queries
    Replies: 35
    Last Post: 05-29-2009, 04: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