【问题标题】:finding missing parent from oracle hierarchical query从 oracle 分层查询中查找丢失的父级
【发布时间】:2014-04-04 06:06:12
【问题描述】:

我有一张具有父/子关系的表。如何找到失踪的父母?

层次表:

child          parent
2000698835     2001455376

如果客户表中不存在父级,我需要插入它。在这里我如何在客户表中找到丢失的父级?

select * from customer where true_gcdb_source_key='2001455376'(which is the parent)

查询所有孩子的父母 从 sap_cust_rel_init 中选择 * 从 child_gcdb_id='2002615591' 开始 通过 child_gcdb_id=prior parent_gcdb_id 连接

child_gcdb_id parent_gcdb_id
2002615591  2002554170
2002554170  2002554286
2002554286  2002554081
2002554081  
2002554081  
2002554081  
2002554286  2002554081
2002554081  
2002554081  
2002554081  
2002554286  2002554081
2002554081  
2002554081  
2002554081  
2002554170  2002554286
2002554286  2002554081
2002554081  
2002554081  
2002554081  
2002554286  2002554081
2002554081  
2002554081  
2002554081  
2002554286  2002554081
2002554081  
2002554081  
2002554081  
2002554170  2002554286
2002554286  2002554081
2002554081  
2002554081  
2002554081  
2002554286  2002554081
2002554081  
2002554081  
2002554081  
2002554286  2002554081

这用于获取该孩子的所有父母

如果客户表中不存在任何父母,我需要插入它们

【问题讨论】:

  • 请详细说明您真正需要什么

标签: sql oracle


【解决方案1】:

这里不需要分层查询,简单的SQL查询就可以了:

select c1.true_gcdb_target_key, c1.true_gcdb_source_key 
from customer c1
where not exists
(
  select 1
  from customer c2
  where c2.true_gcdb_target_key = c1.true_gcdb_source_key
);

我假设您的子字段是true_gcdb_target_key。我只搜索不作为子 ID 出现的父 ID。仅当您需要行之间的层次关系时才使用层次查询,根据层次级别、路径、根...这里您的表已经包含父子信息。

【讨论】:

    猜你喜欢
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    相关资源
    最近更新 更多