Oracle SQL分层查询从下到上的元素

时间:2017-07-07 15:24:40

标签: sql oracle hierarchical

在我的表格中,我存储了每个条目的后继者。

+----+-----------+--+
| ID | SUCCESSOR |  |
+----+-----------+--+
|  1 |         2 |  |
|  2 |         3 |  |
|  3 |           |  |
+----+-----------+--+

我需要从 ID 3 ID 1

我试图通过以下查询来实现此目的,但这不起作用。 : - (

import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
# Preparing random dataFrame with two colums, viz., random x and lag-1 values 
lst1 = list(np.random.rand(100))
df = pd.DataFrame({'x1':lst1})
df['x2'] = df['x1'].shift(1)
df = df[df['x2'] > 0]
# Plotting now
pplot = sns.pairplot(df, kind="reg")

plt.show()

有人可以给我一些建议如何让这个工作吗?

1 个答案:

答案 0 :(得分:0)

以下版本也应该提供正确的答案:

with my_table as 
(select 1 id, 2 successor from dual union
select 2 id, 3 successor from dual union
select 3 id, null successor from dual )
SELECT id FROM my_table
WHERE level = 3
CONNECT BY successor = PRIOR id 
START WITH successor is null
;