SQL recursive query (Oracle) -
i'm having problem recursive query in oracle.
i'm able result using cte in sql server, have adapt code oracle.
the code i'm using is
with n(portefeuille, client, level, opcvm, ca, iter) ( select portefeuille, client, 0 level, opcvm, ca, cast(',' + cast(portefeuille varchar) + ',' varchar) iter dbo.chiffres opcvm null union select m.portefeuille, n.client, n.level+1, m.opcvm, m.ca + n.ca, cast(iter + cast(m.portefeuille varchar) + ',' varchar) iter dbo.chiffres m inner join n on n.portefeuille = m.opcvm ) select distinct n.level, n.client, n.portefeuille, n.ca , n.opcvm, iter, (select count(*) n t1 t1.iter '%,' + cast(n.portefeuille varchar) + ',%') - 1 subordinates n, dbo.chiffres c ((select count(*) n t1 t1.iter '%,' + cast(n.portefeuille varchar) + ',%') - 1) = 0 order level
i think have use 'connect by' in oracle, i'm not sure how use it.
in fact, in final code, sould use "select....." instead of table dbo.chiffres.
the result of "select...." (juste exemple)
client | portefeuille | ca | opcvm 31054 | 024 | 140 | null 104900 | 034 |200 | 024 31054 | 006 | 10 | null
and final result want is
level | client | portefeuille | ca | iter | subordinates 0 | 31054 | 034 | 340 | ,024,034, |0 1 | 31054 | 006 | 10 | ,006, |0
something this:
with n(portefeuille, client, lvl, opcvm, ca, iter) ( select portefeuille, client, 0, opcvm, ca, ',' || portefeuille dbo.chiffres opcvm null union select m.portefeuille, n.client, n.lvl+1, m.opcvm, m.ca+n.ca, iter || m.portefeuille || ',' dbo.chiffres m inner join n on n.portefeuille = m.opcvm ) select distinct n.lvl, n.client, n.portefeuille, n.ca , n.opcvm, iter, ( select count(*) n t1 t1.iter '%,' || n.portefeuille || ',%' ) - 1 subordinates n, dbo.chiffres c ((select count(*) n t1 t1.iter '%,' || n.portefeuille || ',%') - 1) = 0 order lvl
i haven't got tables test on i've
- renamed
level
lvl
(aslevel
keyword used in hierarchical queries). - the string concatenation operator
||
in oracle rather+
. - removed casts - don't think need them.
Comments
Post a Comment