简体   繁体   中英

MS Access Totals Query with Count Function Question

I have a table of member benefit elections. I am trying to determine the amount of records that are of PERSON_TYPE "Child" that have the same Subscriber_number (which would be the subscribing parents SSN) and the same [Date of Birth] but different Member_ID (which would be their SSN). Basically I am looking for twins, triplets, etc. I'm not good with Totals queries in Ms Access but my attempt is below. It returns zero records but I know that there are multiple sets of twins and triplets in the table.

SELECT ClientTable1.PERSON_TYPE, ClientTable1.Subscriber_number, ClientTable1.[Date of Birth], Count(ClientTable1.[Date of Birth]) AS [CountOfDate of Birth]
FROM ClientTable1
WHERE (((ClientTable1.Member_ID)<>[ClientTable1].[Member_ID]))
GROUP BY ClientTable1.PERSON_TYPE, ClientTable1.Subscriber_number, ClientTable1.[Date of Birth]
HAVING (((ClientTable1.PERSON_TYPE)="Child"));

your where clause is wrong,

((ClientTable1.Member_ID)<>[ClientTable1].[Member_ID]) condition will never be true.

you are asking about the same field so it cannot be hit.

instead use the following code:

SELECT ct1.PERSON_TYPE, ct1.Subscriber_number, ct1.[Date of Birth], Count(ct1.[Date of Birth]) AS [CountOfDate of Birth]
FROM ClientTable1 ct1
INNER JOIN ClientTable1 ct2
    ON ct1.PERSON_TYPE = ct2.PERSON_TYPE
    AND ct1.Subscriber_number = ct2.Subscriber_number
    AND ct1.[Date of Birth] = ct2.[Date of Birth]
WHERE (((ct1.Member_ID)<>ct2.[Member_ID]))
GROUP BY ct1.PERSON_TYPE, ct1.Subscriber_number, ct1.[Date of Birth]
HAVING (((ct1.PERSON_TYPE)="Child"));

in this way you are using the inner join in order to check multiple records according to the relation between the tables

and the where condition check that it is not the same Member_ID

You can use the following query and this would give you the the details of the twins/triplets

select t1.PERSON_TYPE, t1.Subscriber_number,
 t1.[Date of Birth], t1.Member_ID from ClientTable1 t1
 join ClientTable1 t2 on t1.Subscriber_number=t2.Subscriber_number
    where t1.Subscriber_number=t2.Subscriber_number
    and t1.[Date of Birth]=t2.[Date of Birth]
    and t1.Member_ID<>t2.Member_ID
order by t1.Subscriber_number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM