我遇到了递归查询的问题,我无法解决。 问题是:我想在下表中获得企业客户帐户树:
header table: accounts {id,code,account,dad_id}
id is PK and dad_id is FK references id
{1,1,'property',null}
{2,2,'for result',null}
{3,1,'actives',1}
{4,2,'passives'1}
{5,1,'not attributable active',3 }
我的递归sql和构建树的问题。
WITH TEMP(id,code,account,dad_id) AS(
SELECT id,code,account,dad_id
FROM FINAL.ACCOUNT a
WHERE a.dad_id IS NOT NULL
UNION ALL
SELECT a.id,a.code,a.account,t.dad_id
FROM FINAL.ACCOUNT a, TEMP t
WHERE a.dad_id=t.dad_id
)
select * from TEMP;
预期结果 代码
1.0 property
1.1 Actives
1.1.1 Non atribbutable active
1.2 Passive
2.0 for result
非常感谢你阅读,我对所有答案持开放态度 感谢