1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| WITH Formule AS
(
SELECT 1 id, '+' signe, 2 ssformid_gauche, 3 ssformid_droit, NULL const FROM dual union ALL
SELECT 2 , NULL , NULL , NULL , 1 FROM dual union ALL
SELECT 3 , '*' , 4 , 5 , NULL FROM dual union ALL
SELECT 4 , '*' , 6 , 7 , NULL FROM dual union ALL
SELECT 5 , '*' , 8 , 9 , NULL FROM dual union ALL
SELECT 6 , NULL , NULL , NULL , 2 FROM dual union ALL
SELECT 7 , NULL , NULL , NULL , 3 FROM dual union ALL
SELECT 8 , NULL , NULL , NULL , 4 FROM dual union ALL
SELECT 9 , NULL , NULL , NULL , 5 FROM dual
)
, SR1 AS
(
SELECT level
, rownum AS rn
, id
, prior id
, coalesce(signe, to_char(const)) AS ope
, connect_by_isleaf AS cbi
FROM formule f
START WITH ID = 1
CONNECT BY ID IN (prior ssformid_gauche, prior ssformid_droit)
)
, SR2 AS
(
SELECT sr1.*
, case
when cbi = 0
AND lead(cbi, 1) over(ORDER BY rn ASC) = 1
then lead(rn , 1) over(ORDER BY rn ASC) + id / 100
when cbi = 0
AND lead(cbi, 1) over(ORDER BY rn ASC) = 0
then lead(rn , 3) over(ORDER BY rn ASC) + id / 100
else rn
end ord
FROM sr1
)
SELECT REPLACE(REPLACE(XMLAgg(XMLElement("x", ope) ORDER BY ord ASC), '</x>', ''), '<x>', '') AS formule
FROM sr2;
FORMULE
---------
1+2*3*4*5 |
Partager