对彼此关系不确定的条目进行计数

发布于 2024-12-16 00:08:54 字数 792 浏览 3 评论 0原文

我想计算数据库中所有与一个人都有指定关系的条目列表,但我需要根据它们彼此之间的关系进行计数 - 换句话说,他们被列为家里主要人物的儿子和女儿、兄弟姐妹——我需要计算兄弟姐妹的数量。

我的方法是尝试统计所有有一个以上儿子或一个女儿的家庭,或者有一个儿子和一个女儿的所有家庭,或者有一个兄弟和一个姐妹、一个以上兄弟或一个以上姐妹的所有家庭。然而,当我尝试将其放入 SQL 中时,我一无所获。

SELECT FirstName, Surname 
FROM individuals
WHERE (RelationshipToHead = 'Son' AND RelationshipToHead = 'Daughter') 
   OR (RelationshipToHead = 'Son' AND RelationshipToHead = 'Son') 
   OR (RelationshipToHead = 'Daughter' AND RelationshipToHead = 'Daughter') 
   OR (RelationshipToHead = 'Brother' AND RelationshipToHead = 'Sister') 
   OR (RelationshipToHead = 'Sister' AND RelationshipToHead = 'Sister') 
   OR (RelationshipToHead = 'Brother' AND RelationshipToHead = 'Brother')

这仅返回包含儿子、女儿、兄弟或姐妹的所有条目,而不是每个家庭中每个条目都有一个以上的条目。

您能建议一个更好的查询吗?

I want to count a list of entries in my database that all have specified relationships to one person, but I need to count by their relationship to each other -
in other words, they are listed as sons and daughters, brothers and sisters of the main person in the house - I need to count the number of siblings.

My approach was to try to count all the households with more than one son or daughter or any household with a son and a daughter or any household with a brother and a sister, more than one brother or more than one sister. However when I try to put this into SQL I'm not getting anywhere.

SELECT FirstName, Surname 
FROM individuals
WHERE (RelationshipToHead = 'Son' AND RelationshipToHead = 'Daughter') 
   OR (RelationshipToHead = 'Son' AND RelationshipToHead = 'Son') 
   OR (RelationshipToHead = 'Daughter' AND RelationshipToHead = 'Daughter') 
   OR (RelationshipToHead = 'Brother' AND RelationshipToHead = 'Sister') 
   OR (RelationshipToHead = 'Sister' AND RelationshipToHead = 'Sister') 
   OR (RelationshipToHead = 'Brother' AND RelationshipToHead = 'Brother')

This merely returns all entries that contain son daughter brother or sister - not the ones with more than one of each in each family.

Could you suggest a better query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

入怼 2024-12-23 00:08:54

where 子句仅比较每行中的值,这就是您的查询未按您想要的方式运行的原因。我建议:

SELECT count(distinct AddressID) HouseholdsWithSiblings, 
       sum(NumberOfSiblings) TotalSiblings
FROM   (SELECT AddressID, count(*) NumberOfSiblings 
        FROM individuals
        WHERE RelationshipToHead in ('Son','Daughter')
        GROUP BY AddressID
        HAVING count(*) > 1
        UNION ALL
        SELECT AddressID, count(*)+1 NumberOfSiblings 
        FROM individuals
        WHERE RelationshipToHead in ('Brother','Sister')
        GROUP BY AddressID) v;

The where clause only compares values within each row, which is why your query isn't behaving as you want. I suggest:

SELECT count(distinct AddressID) HouseholdsWithSiblings, 
       sum(NumberOfSiblings) TotalSiblings
FROM   (SELECT AddressID, count(*) NumberOfSiblings 
        FROM individuals
        WHERE RelationshipToHead in ('Son','Daughter')
        GROUP BY AddressID
        HAVING count(*) > 1
        UNION ALL
        SELECT AddressID, count(*)+1 NumberOfSiblings 
        FROM individuals
        WHERE RelationshipToHead in ('Brother','Sister')
        GROUP BY AddressID) v;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文