仅获取已更新记录的层次结构

3

我有几个表格,其中包含一些资源数据。

Resource
----------
rID |  rname | updatedstamp
R1    Res1      01-Jul-2015
R2    Res2      01-Jul-2015
R3    Res3      01-Jul-2015
R4    Res4      01-Jul-2015
R5    Res5      01-Jul-2015
R15   Res15      01-Aug-2015

ResourceTree
----------
parID | rID  | updatedStamp
---------------------------
NULL  R1       01-Jul-2015
R1    R2       01-Aug-2015
R2    R3       01-Jul-2015
R3    R4       01-Jul-2015  
R4    R5       01-Jul-2015
R14   R15      01-Jul-2015

我需要一条选择查询语句,它将获取所有于“2015年8月1日”或之后更新的记录。此外,我需要获取任何父级的子资源的详细信息,这些资源在“2015年8月1日”上更新。
因此,在我的情况下,我需要从资源表中提取所有在“2015年8月1日”更新的记录。在我的情况下,只有R15。 此外,它还应该从Resource_Tree表中获取任何在“2015年8月1日”或之后进行更新的详细信息。在我的情况下,它将是R2 R3 R4 R5。
结果:
parid rid rname
R14   R15 Res15
R1    R2  Res2
R2    R3  Res3
R3    R4  Res4
R4    R5  Res5    

目前尝试的查询

SELECT RT.ParID,R.ID,R.Rname
FROM RESOURCES R, RESOURCETREE RT
WHERE R.RID = RT.RID  
And (R.UpdatedStamp >= '01-Aug-2015' or RT.UpdatedStamp  >= '01-Aug-2015')
START WITH RT.ParID ='R1'  AND 
CONNECT BY PRIOR RT.RID=RT.ParID 

我认为应该是 CONNECT BY PRIOR RT.ParID = RT.RID - Little Santi
2个回答

0

这可能不是一个优雅的解决方案;

with temp_tbl_1 as (
   -- get all the "rid" updatedstamp >= '01-Aug-2015' from both tables
  select rID
  from ResourceTree
  where updatedstamp >= '01-Aug-2015'
  union all
  select rid
  from Resource
  where updatedstamp >= '01-Aug-2015'
),
temp_tbl_2 as (
    select parID, rID
    from ResourceTree
    START WITH rID in (select distinct rID from temp_tbl_1)
    CONNECT BY PRIOR RID = ParID    
)
select t.parID, t.rID, r.rname 
from temp_tbl_2 t
join Resource r
on r.rID = t.rID

0
with x as (
select rid from ResourceTree
where updatedstamp >= '01-Aug-2015'
union
select rid from Resourc
where updatedstamp >= '01-Aug-2015'
)
select r.parid, r.rid, re.rname
from ResourceTree r 
left join x on r.parid = x.rid
left join Resourc re on re.rid = r.rid
where r.parid is not null

cte 中选择 01-Aug-15 之后的 rid,然后在 ResourceTree 表上进行左连接其他表。


这是不正确的。如果我的资源树中有一个条目为“R16”、“R17”、“2015年7月1日”的话,你的代码就会出错。 - mhn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接