2017-08-24 113 views
0

我有一個蜂巢表如下:分層更新在蜂巢

Table A 

docid corr_docid header 

100     a 
101   100  b 
102     c 
105   101  d 
106   102  e 
107   106  f 
108   107  g 
109     h 

是否有可能創造另一個表。

這裏corr_docid 107校正該文檔與文檔ID 107

表B如下:

Table A 

docid corr_docid header newdocid 

100     a   105 
101   100  b   105 
102     c   108 
105   101  d   105 
106   102  e   108 
107   106  f   108 
108   107  g   108 
109     h   109 

這是可能的配置單元。

+0

無法清楚地理解您的要求,但是您是否嘗試了lead/lag/last_value函數? –

回答

0

您可以試試這個本機SQL來獲得所需的結果,只有當您知道層次結構深度/級別時,這纔會工作。

`select a.docid, 
a.corr_docid, 
case when b.docid is null then a.docid 
when c.docid is null then b.docid 
when d.docid is null then c.docid 
else d.docid 
end newdocid 
from Table_A a left join Table_A b on a.docid = b.corr_docid 
left join Table_A c on b.docid = c.corr_docid 
left join Table_A d on c.docid = d.corr_docid ;` 
+0

使用COALESCE重寫'newdocid' –

+0

是否可以使用此鏈接中提供的CTE實現此目的:https://www.linkedin.com/pulse/step-by-step-guide-creating-sql-hierarchical-queries- bibhas - 米特拉 –