SQL-3级层次结构查询与CONNECT BY?

时间:2011-12-04 11:27:43

标签: sql database oracle hierarchy

假设您有3级等级,如下所示:家庭(1米) - >人(1-m) - >帐户

但只有2个表:

人(3列):

Person_id, person_name, household_key

帐户(3列):

Account_id, account_name, person_id

有哪些方法可以获得返回这些结果的查询(即按家庭,人员和帐户排序):

Household_key     Person_id     Account_id
1                     1             456              
1                     2             763
1                     2             801
1                     2             822
2                     3              54
2                     4              68

1 个答案:

答案 0 :(得分:3)

从对我来说看起来不像层次结构查询的期望结果判断 - 尝试

SELECT DISTINCT
P.Household_key,
P.Person_id,
A.Account_id
FROM
Person P
INNER JOIN Account A ON P.Person_id = A.Person_id
ORDER BY 
P.Household_key,
P.Person_id,
A.Account_id

如果我弄错了,那么您需要在表PersonAccount中显示一些示例数据。