0

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
3
  • CTEs work in Oracle (it is called a sub-query factoring clause in the documentation) - you should be able to port it fairly easily. Rename level to lvl - level is a keyword used in hierarchical queries and cannot be used as an alias. Commented Jun 11, 2016 at 0:52
  • Thank you for your answer. I think i tried this solution, but i got the error 'missing SELECT keyword'. In fact my problem is a little bit more complicated because i don't have a table dbo.chiffres but a 'select ....' to use instead. Can this be the source of the problem? Commented Jun 11, 2016 at 13:17
  • I added some details to my post to explane more my problem. Commented Jun 11, 2016 at 15:00

1 Answer 1

1

Something like this:

WITH n(portefeuille, client, lvl, opcvm, ca, iter) AS (
  SELECT  portefeuille,
          client,
          0,
          opcvm,
          ca,
          ',' || portefeuille
  FROM    dbo.chiffres
  WHERE   opcvm is null 
UNION ALL
  SELECT  m.portefeuille,
          n.client,
          n.lvl+1,
          m.opcvm,
          m.ca+n.ca,
          iter || m.portefeuille || ','
FROM      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(*)
                FROM n T1
                WHERE T1.iter LIKE '%,' || n.portefeuille || ',%'
       ) - 1 AS Subordinates
from   n,
       dbo.chiffres c
where ((SELECT COUNT(*) FROM n T1 WHERE T1.iter LIKE '%,' || n.portefeuille || ',%') - 1) = 0
order by lvl

I haven't got any tables to test it on but I've

  • renamed level to lvl (as level is a keyword used in hierarchical queries).
  • The string concatenation operator is || in Oracle rather than +.
  • Removed all the casts - I don't think you need them.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.