【问题标题】:SQL Server - count the number of living grandparentsSQL Server - 计算在世祖父母的数量
【发布时间】:2020-03-21 15:13:35
【问题描述】:

我正在家庭学习数据库,目前正在尝试解决以下问题:

选择孙辈的 ID 和姓名。同时选择 每个人还有他们还活着的祖父母的人数。

我的桌子看起来如下:

id, name, date_of_birth, date_of_death(当这个人是 NULL 活着),性别,father_id,mother_id

我能够通过以下方式选择孙辈的 id 和 name 来解决这个问题:

SELECT b.name, b.id
FROM persons a
JOIN persons b ON c.father_id = a.id or a.mother_id = a.id
JOIN persons c on p.father_id = c.id 
WHERE b.father_id = a.id or b.mother_id = a.id;

但是,我无法解决每个人还活着的祖父母数量的部分。

你能帮帮我吗?

谢谢

【问题讨论】:

  • 如果你创建一个 SQL fiddle,它会让人们更容易回答。我创建了一个你可以根据需要分叉/修改的。我建议在您的问题中包含此链接。 sqlfiddle.com/#!9/441ad0/7

标签: mysql sql sql-server database


【解决方案1】:

这是一个加入表格的问题。我想你们都对自联接上的表别名感到困惑。所以给他们有意义的别名:

  • p 为人
  • pp 代表该人的父母
  • gp 给这个人的祖父母

那么,一旦你的JOINs 正确,剩下的主要是聚合:

SELECT p.name, p.id,
       SUM(CASE WHEN gp.date_of_death IS NULL THEN 1 ELSE 0 END)
FROM persons p JOIN    -- persons
     persons pp        -- parents
     ON pp.id IN (p.father_id, p.mother_id) JOIN
     persons gp
     ON gp.id IN (pp.father_id, pp.mother_id)
GROUP BY p.name, p.id;

注意条件总和,其中包括死亡日期。

【讨论】:

  • 比我的回答优雅得多。 +1
【解决方案2】:

你需要加入3份表格,按人分组,有条件地统计在世祖父母的数量:

SELECT p.id, p.name, 
  count(case when p2.date_of_death is null then 1 end) living_grandparents 
FROM persons p
JOIN persons p1 ON p1.id IN (p.father_id, p.mother_id) 
JOIN persons p2 on p2.id IN (p1.father_id, p1.mother_id) 
GROUP BY p.id, p.name

对于 MySql,条件可以简化为:

sum(p2.date_of_death is null) living_grandparents 

【讨论】:

    【解决方案3】:

    查看 SQL Fiddle 以获得解决方案 :)

    http://sqlfiddle.com/#!9/441ad0/16

        select 
      (
        select 
          count(*) 
        from 
          people 
        where 
          (
            id = p.fatherId 
            or id = p.motherId
          ) 
          and (
            fatherId in (
              select 
                id 
              from 
                people 
              where 
                dod is null
            ) 
            or motherId in (
              select 
                id 
              from 
                people 
              where 
                dod is null
            )
          )
      ) as mycount, 
      p.name, 
      p.id, 
      p.fatherId, 
      p.motherId 
    FROM 
      people p 
    WHERE 
      p.fatherId in (
        SELECT 
          id 
        from 
          people 
        where 
          id = p.fatherId 
          and fatherId in (
            SELECT 
              id 
            from 
              people 
            where 
              1 = 1
          )
      ) 
      OR p.motherId in (
        SELECT 
          id 
        from 
          people 
        where 
          id = p.motherId 
          and motherId in (
            SELECT 
              id 
            from 
              people 
            where 
              1 = 1
          )
      )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多