I'm having a problem with a recursive query in Oracle.
I'm currently able to get the result by using CTE in SQL Server, but I have to adapt the code for Oracle.
The code I'm using is
WITH n(portefeuille, client, level, opcvm, ca, iter) AS
(
SELECT
portefeuille, client, 0 as level, opcvm, ca,
CAST(',' + CAST(portefeuille AS varchar) + ',' AS varchar) AS iter
FROM
dbo.chiffres
WHERE
opcvm IS NULL
UNION ALL
SELECT
m.portefeuille, n.client, n.level+1, m.opcvm, m.ca + n.ca,
CAST(iter + CAST(m.portefeuille AS varchar) + ',' AS varchar) AS iter
FROM
dbo.chiffres AS m
INNER JOIN
n ON n.portefeuille = m.opcvm
)
SELECT DISTINCT
n.level, n.client, n.portefeuille, n.ca , n.opcvm, iter,
(SELECT COUNT(*)
FROM n T1
WHERE T1.iter LIKE '%,' + CAST(n.portefeuille AS varchar) + ',%') - 1 AS Subordinates
FROM
n, dbo.chiffres c
WHERE
((SELECT COUNT(*)
FROM n T1
WHERE T1.iter LIKE '%,' + CAST(n.portefeuille AS varchar) + ',%') - 1) = 0
ORDER BY
level
I think that I have to use 'connect by' in oracle, but I'm not sure how to use it.
In fact, in my final code, I sould use a "select....." instead of the table dbo.chiffres.
The result of this "select...." is (juste an exemple)
Client | portefeuille | CA | OPCVM
31054 | 024 | 140 | NULL
104900 | 034 |200 | 024
31054 | 006 | 10 | NULL
And the final result that i want is
level | Client | portefeuille | CA | iter | subordinates
0 | 31054 | 034 | 340 | ,024,034, |0
1 | 31054 | 006 | 10 | ,006, |0
leveltolvl-levelis a keyword used in hierarchical queries and cannot be used as an alias.