如何选择有关系的最早日期?

时间:2019-10-09 10:22:07

标签: sql sqlite

我有这些表:

    create table persons (
      fname     char(12),
      lname     char(12),
      bdate     date,
      bplace    char(20),
      address   char(30),
      phone     char(12),
      primary key (fname, lname));

    create table births (
      regno     int,
      fname     char(12),
      lname     char(12),
      regdate   date,
      regplace  char(20),
      gender    char(1),
      fthr_fname    char(12),
      fthr_lname    char(12),
      mthr_fname    char(12),
      mthr_lname    char(12),
      primary key (regno),
      foreign key (fname,lname) references persons,
      foreign key (fthr_fname,fthr_lname) references persons,
      foreign key (mthr_fname,mthr_lname) references persons); 

我只需要查找给定fname和lname的最老孩子的名字和姓氏(在我的情况下分别为“ Michael”和“ Fox”)。

只有在有一个大孩子的情况下,我才取得成功。

但是,如果最大的孩子是TWIN / TRIPLETS / ETC,则需要显示所有孩子的名字和姓氏。我将如何处理?

一些示例数据:

insert into persons values ('Michael', 'Fox', '1961-06-09', 'Edmonton, AB', 'Manhattan, New York, US', '212-111-1111');
insert into persons values ('Tracy', 'Pollan', '1960-06-22', 'Long Island, New York, US', 'Manhattan, New York, US', '212-222-1112');
insert into persons values ('TwinOne', 'Fox', '1995-11-20', 'Los Angeles, CA, US', 'Manhattan, New York, US', '212-222-1113');
insert into persons values ('TwinTwo', 'Fox', '1995-11-20', 'Los Angeles, CA, US', 'Manhattan, New York, US', '212-222-1114');
insert into persons values ('Young', 'Fox', '1997-07-06', 'Manhattan, New York, US', 'Manhattan, New York, US', '212-222-1115');
insert into births values (310, 'TwinOne', 'Fox', '1995-11-20', 'Los Angeles, CA, US', 'M', 'Michael', 'Fox', 'Tracy', 'Pollan');
insert into births values (312, 'TwinTwo', 'Fox', '1995-11-20', 'Los Angeles, CA, US', 'F', 'Michael', 'Fox', 'Tracy', 'Pollan');
insert into births values (314, 'Young', 'Fox', '1997-07-06', 'Manhattan, New York, US', 'M', 'Michael', 'Fox', 'Tracy', 'Pollan');

我刚得到一个大孩子的条件:

SELECT p.fname, p.lname
FROM births b1, births b2, persons p
WHERE b1.fname = 'Michael' AND b1.lname = 'Fox'
AND b2.f_fname = b1.fname AND b2.f_lname = b1.lname
AND p.fname = b2.fname AND p.lname = b2.lname
ORDER BY julianday(p.bdate) ASC
LIMIT 1

我在上面的代码中得到的结果:

fname       lname
----------  ----------
TwinOne     Fox

我想要的结果

fname       lname
----------  ----------
TwinOne     Fox
TwinTwo     Fox

3 个答案:

答案 0 :(得分:0)

declare @firstName varchar(100)
declare @lastName varchar(100)
set @firstName = 'Michael'
set @lastName = 'Fox'

select child.fname, child.lname
from births child left join persons father on child.fthr_fname = father.fname and child.fthr_lname = father.lname
    left join persons mother on child.mthr_fname = mother.fname and child.mthr_lname = mother.lname
where (father.fname = @firstName and father.lname = @lastName) or (mother.fname = @firstName and mother.lname = @lastName) 
group by child.fname, child.lname, child.regdate
having child.regdate = (select min(regdate) from births where fthr_fname = @firstName and fthr_lname = @lastName)

答案 1 :(得分:0)

使用Sqlite 3.25中添加的窗口函数的一种方法:

WITH offspring AS
 (SELECT fname, lname
  FROM births
  WHERE (fthr_fname, fthr_lname) = ('Michael', 'Fox')
     OR (mthr_fname, mthr_lname) = ('Michael', 'Fox'))
SELECT fname, lname
FROM (SELECT p.fname, p.lname, p.bdate, rank() OVER (ORDER BY p.bdate) AS rnk
      FROM persons AS p
      JOIN offspring AS o ON (p.fname, p.lname) = (o.fname, o.lname))
WHERE rnk = 1;
fname       lname     
----------  ----------
TwinOne     Fox       
TwinTwo     Fox       

基本上:对于出生表中每条父母姓名等于Michael Fox的记录,请按出生日期对其进行排名,并且仅返回排名1-最旧的记录。

答案 2 :(得分:0)

使用CTE返回该人的所有孩子('Michael Fox'),然后返回NOT EXISTS

with children as (
  select p.fname, p.lname, p.bdate
  from persons p inner join births b
  on p.fname = b.fname and p.lname =b.lname
  where (b.fthr_fname = 'Michael' and b.fthr_lname = 'Fox') 
  or (b.mthr_fname = 'Michael' and b.mthr_lname = 'Fox')
)
select c.* from children c
where not exists (
  select 1 from children
  where bdate < c.bdate
) 

请参见demo
结果:

| fname   | lname | bdate      |
| ------- | ----- | ---------- |
| TwinOne | Fox   | 1995-11-20 |
| TwinTwo | Fox   | 1995-11-20 |