Hi Tom,
We are using SYS_CONNECT_BY_PATH function to get all possible combination of a column having quantity.
Problem we are facing is , it is not considering the repeated values for that column like if there are values like 30,15,15,20
possible combinations would be
Combinations with 2 values
30 - 15 (1st 15)
30 - 15 (2nd 15)
30 - 20
15 (1st 15) - 15 (2nd 15)
15 (1st 15) - 20
15 (2nd 15) - 20
Combinations with 3 values
30 , 15 (1st 15) ,15 (2nd 15)
30 , 15 (1st 15), 20
30 , 15 (2nd 15), 20
15 (1st 15) ,15 (2nd 15), 20
Combinations with 4 values
30 , 15 (1st 15) ,15 (2nd 15), 20
So ideally these combinations are possible. But query
select sys_connect_by_path(QTY, ',' ) colvalues
from (select '('||TO_NUMBER(QTY)||')' QTY
from TABLE X)
connect by QTY > prior QTY
is givng me follwoing output.
,(30)
,(20)
,(20),(30)
,(-15)
,(-15),(30) -- 1st 15
,(-15),(20) -- 1st 15
,(-15),(20),(30) -- 1st 15
,(-15)
,(-15),(30) -- 2nd 15
,(-15),(20) -- 2nd 15
,(-15),(20),(30) -- 2nd 15
So question here is , why it is not giving me combination
30 , -15 , - 15 or -15 , -15 or -15, -15, 20
as neither in any of the combination both -15 are present.
because you said "qty > prior qty"
15 = 15
15 is not greater than or less than 15, I don't know why you would expect that to be there given the predicate you applied.
This is not going to work with connect by. You want to connect each row to every other row such that the qty >= prior qty and we haven't already connected to that row in the hierarchy
ops$tkyte%ORA10GR2> select qty, sys_connect_by_path( qty, ',' ) scbp
2 from (
3 select qty, row_number() over (order by qty) rn
4 from t
5 )
6 connect by prior qty >= qty and rn < prior rn
7 order by level
8 /
QTY SCBP
---------- ------------------------------
15 ,15
15 ,15
30 ,30
20 ,20
15 ,20,15
15 ,20,15
15 ,30,15
15 ,15,15
20 ,30,20
15 ,30,15
15 ,30,15,15
15 ,30,20,15
15 ,30,20,15
15 ,20,15,15
15 ,30,20,15,15
15 rows selected.