数数功能孩子和孙子,我生孩子了,我怎么生孙子

时间:2019-08-22 12:32:49

标签: sql sql-server function count

im试图在狗数据库中获取孙子的数量,并且只希望将有15个或更多孩子和/或40个或更多孙子的狗导出到列表中。我不需要仅列举其中几个孩子或孙子的名字。

我制作了一个小提琴数据库以显示我得到的票价。

新更新于2019.08.23 kl 16:31 https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=9f700055c3d0a066b545f9f489fb1801

我已建立并无法更改数据库的旧数据库,因此我必须使用已有的数据库。我已经尝试了一些在google上找到的方法,但是它们似乎都是针对更复杂的数据库的,或者我没有得到它们(很可能是最后一个,因为我不是很好,sql)

如果任何人有任何提示或建议,将不胜感激。 如果我对sql有了更多的了解,我可能可以用更好的方式解释这个问题

select d2.Id ,d2.Reg , d2.Name , count(d2.id) as Children

from dbo.Dogs d 

left join dbo.Litters D0 on D0.Id = d.LitterId

left join dbo.Dogs D1 on D0.FatherId=D1.ID

left join dbo.Dogs D2 on D0.MotherId=D2.ID

Group by d2.id , d2.name , d2.Reg

having COUNT(*) > 12

这就是我得到的:

Id  Reg Name    Children

1   NO23456/2009    Queen   20

8   NO23457/2009    Godess  14

我想要的是

Id  Reg     Name    Children    Grandchildren

1   NO23456/2009    Queen   20      0

8   NO23457/2009    Godess  14      0

我要在这里完成的一个查询是,我可以在母亲的一侧定义一些幼犬,在父亲的一侧定义另一个幼犬。并将第三个数字添加到双方的子孙

  

创建表dbo.dogs(ID varchar(255),Reg varchar(255),Name varchar(255),LitterID varchar(255));

     

创建表dbo.litters(ID int,Dateofbirth日期,FatherID int,MotherID int);

     

将狗插入狗(id,Reg,NAME,垃圾)

     

值   ('11','NO34567 / 2012','Fido1','9000'),('12','NO34568 / 2012','Fido2','9000'),('13','NO34569 / 2012' ,'Fido3','9000'),('14','NO34570 / 2012','Fido4','9000'),('15','NO34571 / 2012','Fido5','9000'), ('16','NO34572 / 2014','Fido6','7000'),('17','NO34573 / 2014','Fido7','7000'),   ('18','NO34574 / 2014','Fido8','7000'),('19','NO34575 / 2014','Fido9','7000'),('20','NO34576 / 2014' ,'Fido10','7000'),('21','NO34577 / 2014','Fido11','7000'),('22','NO34578 / 2016','Fido12','6000'), ('23','NO34579 / 2016','Fido13','6000'),('24','NO34580 / 2016','Fido14','6000'),   ('25','NO34581 / 2016','Fido15','6000'),('26','NO34582 / 2016','Fido16','6000'),('27','NO34583 / 2016' ,'Fido17','6000'),('28','NO34584 / 2015','Fido18','5000'),('29','NO34585 / 2015','Fido19','5000'), ('30','NO34586 / 2015','Fido20','5000'),('31','NO34587 / 2015','Fido21','5000'),   ('32','NO34588 / 2015','Fido22','5000'),('33','NO34589 / 2015','Fido23','5000'),('34','NO34590 / 2015' ,'Fido24','5000'),('35','NO34591 / 2015','Fido25','5000'),('36','NO34592 / 2015','Fido26','5000'), ('37','NO34593 / 2017','Fido27','4000'),('38','NO34594 / 2017','Fido28','4000'),   ('39','NO34595 / 2017','Fido29','4000'),('40','NO34596 / 2017','Fido30','4000'),('41','NO34597 / 2017' ,'Fido31','4000'),('42','NO34598 / 2017','Fido32','4000'),('43','NO34599 / 2017','Fido33','4000'), ('44','NO34600 / 2017','Fido34','4000'),('45','NO34601 / 2014','Fido35','3000'),   ('46','NO34602 / 2014','Fido36','3000'),('47','NO34603 / 2014','Fido37','3000'),('48','NO34604 / 2014' ,'Fido38','3000'),('49','NO34605 / 2014','Fido39','3000'),('50','NO34606 / 2014','Fido40','3000'), ('51','NO34607 / 2015','Fido41','2000'),('52','NO34608 / 2015','Fido42','2000'),   ('53','NO34609 / 2015','Fido43','2000'),('54','NO34610 / 2015','Fido44','2000'),('55','NO34611 / 2015' ,'Fido45','2000'),('56','NO34631 / 2018','Fido45jr','2001'),('57','NO34641 / 2018','Fido45jr','2001'), ('58','NO34651 / 2018','Fido45jr','2001'),   ('59','NO34661 / 2018','Fido45jr','2001'),('2','NO12345 / 2010','king','8001'),('1','NO23456 / 2009' ,'Queen','7001'),('7','NO12346 / 2010','God','8000'),('8','NO23457 / 2009','Godess,'7001'), ('9','NO12346 / 2010','Devil','8000'),('5','NO12346 / 2011','Princess','9002');

     

插入垃圾(ID,出生日期,父亲ID,母亲ID)

     

值   ('9000','2012.04.01','2','1'),('5000','2015.05.01','2','1'),('6000','2016.06.01' ,'9','1'),('7000','2014.04.02','7','8'),('4000','2017.05.02','2','8'), ('3000','2014.06.02','2','5'),('2000','2015.04.03','9','5'),('2001','2018.04.03' ,“ 23”,“ 55”);

1 个答案:

答案 0 :(得分:0)

如果我了解您的架构权利将起作用:

SELECT d.Id                   AS ParentID,
       d.Name,
       d.Reg,
       COUNT(DISTINCT d1.id)  AS children,
       COUNT(DISTINCT d2.id)  AS grandchildren
FROM   Dogs d
       JOIN dbo.Litters LittersFarther
            ON  LittersFarther.FatherId = d.id
                OR LittersFarther.MotherId = d.id
       LEFT JOIN Dogs d1
            ON  LittersFarther.id = d1.litterid
       LEFT JOIN dbo.Litters Litters2
            ON  Litters2.FatherId = d1.id
                OR Litters2.MotherId = d1.id
       LEFT JOIN Dogs d2
            ON  Litters2.id = d2.litterid
       LEFT JOIN dbo.Litters Litters1
            ON  Litters1.id = d.litterID
WHERE  Litters1.id IS            NULL
GROUP BY
       d.Id,
       d.Name,
       d.Reg
HAVING COUNT(DISTINCT case when LittersFarther.FatherId = d.id then d1.id ELSE 0 end) > 12 -- Filter for number of father's children
       OR COUNT(DISTINCT case when LittersFarther.FatherId = d.id then d2.id ELSE 0 end) > 2  -- Filter for number of father's grandchildren   
       OR COUNT(DISTINCT case when LittersFarther.MotherId = d.id then d1.id ELSE 0 end) > 12  -- Filter for number of mother's children
       OR COUNT(DISTINCT case when LittersFarther.MotherId = d.id then d2.id ELSE 0 end) > 4  -- Filter for number of mother's grandchildren