带有计数功能问题的MS Access总计查询

时间:2018-12-05 06:59:44

标签: sql ms-access

我有一张会员福利选举表。我正在尝试确定PERSON_TYPE“孩子”的记录数量,这些记录具有相同的Subscriber_number(将作为订阅父SSN)和相同的[Date of Birth]但不同的{{ 1}}(将是他们的SSN)。基本上,我正在寻找双胞胎,三胞胎等。我对Access女士中的Totals查询不太满意,但下面有尝试。它返回零条记录,但我知道表中有多对双胞胎和三胞胎。

Member_ID

2 个答案:

答案 0 :(得分:1)

您的where子句错误,

((ClientTable1.Member_ID)<>[ClientTable1].[Member_ID])条件永远不会成立。

您要询问的是同一字段,以免被击中。

请使用以下代码:

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"));

通过这种方式,您正在使用内部联接,以便根据表之间的关系检查多个记录

以及where条件检查它是否不是同一Member_ID

答案 1 :(得分:0)

您可以使用以下查询,这将为您提供双胞胎/三胞胎的详细信息

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