簡體   English   中英

SQL查詢層次結構

[英]SQL Query for a hierarchical structure

我一直在嘗試為分層結構編寫查詢,除了下面提到的方法之外,我還想知道是否有更好的方法編寫此查詢。 病理命令的結構如下:醫院->實驗室->部門->分部->台式/儀器

有一個單獨的表“ resource”,將所有這些列另存為父子關系(child_resource,parent_resource)以及資源類型。 例如

表資源:

  parent_resource    child_resource    resource        resource_type

  DE Hospital        DE Section        DE Lab          Lab
  DE Lab             DF Sub Section    DF Section      Section
  DE Section         DE bench          DF SubSection   Bench
  DE Section         DF bench          DF SubSection   Bench
  DE Section         DG bench          DF SubSection   Bench

另一個表Orders包含這2個表的所有鏈接。

表順序:

  Order_id         resource_Type    Resource  

  12345            SubSection       DF SubSection
  23456            bench            DG bench
  34567            Section          DE Section

我希望結果為

  Order_id   resource         Hospital       Lab      Section      Subsection      Bench      

  12345      DF SubSection    DE Hospital    DE Lab   DE Section   DF SubSection   -
  23456      DG bench         DE Hospital    DE Lab   DE Section   DF SubSection   DG bench
  34567      DE Section       DE Hospital    DE Lab   DE Section     -             -

為了實現上述結果,我可以根據resource_type進行多個左聯接(如果Resource type是subsection,則不捕獲Bench信息),(如果resource type是section,則應僅捕獲Hospital,Lab,section,而不捕獲任何內容)其他信息,即使該信息存在)。

第一個左加入:

left outer join (select *

from resource rg

join resource rg_section on rg.child_resource = 
  rg_section.parent_resource
and rg_section.active_ind=1

join resource_group rg_subsection on rg_subsection.parent_resource = rg_section.child_resource
and rg_subsection.active_ind=1

where rg.active_ind=1
 ) sr_rs on 
 order.resource in (orders.resource_type(subsection))

第2個左聯接:

left outer join (select

from resource rg

join resource rg_section on rg.child_resource = rg_section.parent_resource
and rg_section.active_ind=1

join resource rg_subsection on rg_subsection.parent_resource = 
rg_section.child_resource
and rg_subsection.active_ind=1

join resource rg_bench on rg_bench.parent_resource = 
rg_subsection.child_resource
and rg_bench.active_ind=1

join resource sr on sr.service_resource_cd = rg_bench.child_resource
and sr.active_ind=1

where rg.active_ind=1
 ) sr_rs on 
  order.resource in (orders.resource_type(bench))

這是我的嘗試。 我先建立層次結構,然后將訂單加入其中,並有條件地對適當的列進行分組。 數據的組織方式使任務復雜化,但是最后我得到了預期的結果:

with hierarchy as (
    select r.*, level, connect_by_root(child_resource) root
      from resources r
      connect by prior resource_ = child_resource 
              or resource_ = prior parent_resource) 
select order_id, root, 
       max(case h.resource_type when 'Lab' then h.parent_resource end) hospital,
       max(case h.resource_type when 'Lab' then h.resource_ end) lab,
       max(case h.resource_type when 'Lab' then h.child_resource end) section,
       max(case h.resource_type when 'Section' then h.child_resource end) subsection,
       max(case h.resource_type when 'Bench' then h.child_resource end) bench
  from orders o join hierarchy h on h.root = o.resource_
  group by order_id, root order by order_id

dbfiddle演示

請檢查並測試。 希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM