1

我想创建一个视图,让我拥有一些最年长的人的孙子孙女。问题是我找不到在 sql 中翻译这句话的方法:“谁有一些”。

我只在一张非常基本的桌子上工作:

人:数字(类型编号),姓氏,名字,出生日期,性别,母亲(类型编号),父亲(类型编号)。

这就是我试图做的:

我试图创建一个视图,它给了我最年长的有孙子孙女的人(但这不是这里所做的)

  CREATE OR REPLACE
  VIEW oldestone
  AS SELECT number FROM persons
     WHERE (sysdate-dateofbirth)/365 >= ALL
     (SELECT (sysdate-dateofbirth)/365 FROM persons)
     AND EXISTS (SELECT * FROM persons
                  WHERE level=3 
            START WITH number = number
            CONNECT BY PRIOR number = father OR PRIOR numero = mother);

有了第一个视图的数量,我可以得到孙子:

CREATE OR REPLACE
VIEW grandchildren
AS SELECT firstname,lastname FROM persons
   WHERE level=3 
   START WITH number = (SELECT number FROM oldestone)
   CONNECT BY PRIOR number = father OR PRIOR number = mother;

问题是我知道我不是在翻译:有一些人的最年长的人的孙子孙女。因为在我写 number = number 的第一个视图中,我想引用我的 select 子句第 3 行的编号,但我知道事实并非如此。

在此先感谢您的帮助!

克里斯。

4

2 回答 2

0

如果您反转方向来阅读“向上”家谱,而不是“向下”,您可以从孙子到他们的父母,然后再到祖父母。当你这样做时,在最里面的子查询中,你可以用connect_by_root()操作符“记住”孙子的名字,并将它们与不同级别的祖先的出生日期相关联(他们自己的出生日期在第 1 级,他们父母的出生日期在第 1 级) 2,他们祖父母的出生日期在第 3 级)。

在中间子查询中,我选择由层次查询在第 3 级产生的行——这将显示祖父母的出生日期。我使用分析函数在同一查询中记录最小出生日期。(你不需要“年龄”——“最老”是指最早的出生日期!)

我尽可能经济地编写查询,在每个阶段只保留产生所需结果所需的列(最年长的祖父母的孙辈的名字);当您选择子子查询和中间子查询并分别运行它们时,要了解其工作原理,您可能需要添加更多列以查看发生了什么。

我在 WITH 子句中创建了一些非常简单的测试数据(不是解决方案的一部分);您可能希望使用更有趣的输入进行测试。祝你好运!

with
     person ( id, last_name, first_name, dob, mother, father ) as (
       select  1, 'Doe', 'John', date '1990-03-02',  2,  3 from dual union all
       select  2, 'Doe', 'Anne', date '1962-11-21',  4,  5 from dual union all
       select  3, 'Doe', 'Alan', date '1960-02-23',  6,  7 from dual union all
       select  4, 'Orf', 'Jean', date '1953-10-11',  8,  9 from dual union all
       select  5, 'Orf', 'Stan', date '1952-09-06', 10, 11 from dual union all
       select 22, 'Sun', 'Ryan', date '1968-02-21', 23, 24 from dual union all
       select 23, 'Sun', 'Mary', date '1934-12-09', 26, 27 from dual
     )
--  end of test data; solution (SQL query) begins below this line
select last_name, first_name
from   (
         select last_name, first_name, dob, min(dob) over () as min_dob
         from   (
                  select connect_by_root(last_name)  as last_name ,
                         connect_by_root(first_name) as first_name, dob, level as lvl
                  from   person
                  connect by id in (prior mother, prior father)
         ) 
         where  lvl = 3
)
where  dob = min_dob
;

LAST_NAME  FIRST_NAME
---------  ----------
Doe        John
于 2017-01-03T03:34:49.140 回答
0

桌子:

创建表 FAMILYTREE (id int, name varchar(50), MOTHERID int, FATHERID int); /

插入 FAMILYTREE (id, MOTHERID, FATHERID, name) 值 (1, null, null, 'My Grand Father'); 插入 FAMILYTREE (id, MOTHERID, FATHERID, name) 值 (2, NULL, NULL, 'My Grand Mother'); 插入 FAMILYTREE (ID, MotherID, FatherID, Name) VALUES (3, 10, 9, 'My Mother'); 插入 FAMILYTREE (ID, MotherID, FatherID, Name) VALUES (4, 2, 1, 'My Father'); 插入 FAMILYTREE (ID, MotherID, FatherID, Name) VALUES (5, 3, 4, 'Me'); 插入 FAMILYTREE (ID, MotherID, FatherID, Name) VALUES (6, 12, 11, '我的妻子'); 插入 FAMILYTREE (ID, MotherID, FatherID, Name) VALUES (7, 3, 4, 'My Brother'); 插入 FAMILYTREE (ID, MotherID, FatherID, Name) VALUES (8, 6, 5, 'My son'); 插入 FAMILYTREE (ID, MotherID, FatherID, Name) VALUES (9, NULL, NULL, 'My Mother Grand Father'); 插入 FAMILYTREE (ID, MotherID, FatherID, Name) VALUES (10, NULL, NULL, 'My Mother Grand Mother'); 插入 FAMILYTREE (ID, MotherID, FatherID, Name) VALUES (11, null, null, 'My Wife Grand Father'); 插入 FAMILYTREE (id, MOTHERID, FATHERID, name) 值 (12, null, null, 'My Wife Grand Mother'); 犯罪; / 我的妻子祖母”);犯罪; / 我的妻子祖母”);犯罪; /

数据库:

WITH FamilyCTE(ID,name,MotherID,FATHERID,FatherName,MOTHERNAME,LVL) as ( select f.*, null as FATHERNAME,
null as MOTHERNAME, 0 as LVL from FAMILYTREE F where F.FATHERID is null AND f.MotherID IS NULL UNION ALL SELECT f.ID, f.Name AS ParentName, f.MotherID, F.FATHERID, c.Name AS FatherName, C2.name as MOTHERNAME, lvl + 1 from FAMILYTREE F INNER JOIN FamilyCTE c ON F.FatherID = c。 ID INNER JOIN FamilyTree c2 ON f.MotherID = c2.ID )

从家庭中选择 * ;

于 2021-10-25T05:12:49.620 回答