Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Finding all possible combinations of numbers to reach a given sum

Hi,
I have a table with 66 payment records, but when I run a recursive SQL to find matching sum value(37027.73) from all combination of payment records, the query goes in a infinite loop and it does not come out. Can someone pls help on this.
Table Structure: Create table match_samples (ID NUMBER, PAYMENT NUMBER(14,2));
Table data scripts :
Insert into match_samples (ID,PAYMENT) values (1,2140.57);
Insert into match_samples (ID,PAYMENT) values (2,2140.57);
Insert into match_samples (ID,PAYMENT) values (3,2140.57);
Insert into match_samples (ID,PAYMENT) values (4,2140.57);
Insert into match_samples (ID,PAYMENT) values (5,2140.57);
Insert into match_samples (ID,PAYMENT) values (6,2140.57);
Insert into match_samples (ID,PAYMENT) values (7,2140.57);
Insert into match_samples (ID,PAYMENT) values (8,2140.57);
Insert into match_samples (ID,PAYMENT) values (9,4424.52);
Insert into match_samples (ID,PAYMENT) values (10,4424.52);
Insert into match_samples (ID,PAYMENT) values (11,4424.52);
Insert into match_samples (ID,PAYMENT) values (12,4424.52);
Insert into match_samples (ID,PAYMENT) values (13,4424.52);
Insert into match_samples (ID,PAYMENT) values (14,4424.52);
Insert into match_samples (ID,PAYMENT) values (15,4424.52);
Insert into match_samples (ID,PAYMENT) values (16,4424.52);
Insert into match_samples (ID,PAYMENT) values (17,4424.52);
Insert into match_samples (ID,PAYMENT) values (18,4424.52);
Insert into match_samples (ID,PAYMENT) values (19,3950.12);
Insert into match_samples (ID,PAYMENT) values (20,3950.12);
Insert into match_samples (ID,PAYMENT) values (21,3950.12);
Insert into match_samples (ID,PAYMENT) values (22,3950.12);
Insert into match_samples (ID,PAYMENT) values (23,3950.12);
Insert into match_samples (ID,PAYMENT) values (24,3950.12);
Insert into match_samples (ID,PAYMENT) values (25,3123.64);
Insert into match_samples (ID,PAYMENT) values (26,3123.64);
Insert into match_samples (ID,PAYMENT) values (27,3123.64);
Insert into match_samples (ID,PAYMENT) values (28,3123.64);
Insert into match_samples (ID,PAYMENT) values (29,3123.64);
Insert into match_samples (ID,PAYMENT) values (30,3123.64);
Insert into match_samples (ID,PAYMENT) values (31,3123.64);
Insert into match_samples (ID,PAYMENT) values (32,3123.64);
Insert into match_samples (ID,PAYMENT) values (33,3909.93);
Insert into match_samples (ID,PAYMENT) values (34,3909.93);
Insert into match_samples (ID,PAYMENT) values (35,3909.93);
Insert into match_samples (ID,PAYMENT) values (36,3909.93);
Insert into match_samples (ID,PAYMENT) values (37,3909.93);
Insert into match_samples (ID,PAYMENT) values (38,3909.93);
Insert into match_samples (ID,PAYMENT) values (39,3169.57);
Insert into match_samples (ID,PAYMENT) values (40,3169.57);
Insert into match_samples (ID,PAYMENT) values (41,3169.57);
Insert into match_samples (ID,PAYMENT) values (42,3169.57);
Insert into match_samples (ID,PAYMENT) values (43,3169.57);
Insert into match_samples (ID,PAYMENT) values (44,3169.57);
Insert into match_samples (ID,PAYMENT) values (45,3021.02);
Insert into match_samples (ID,PAYMENT) values (46,3021.02);
Insert into match_samples (ID,PAYMENT) values (47,3021.02);
Insert into match_samples (ID,PAYMENT) values (48,3021.02);
Insert into match_samples (ID,PAYMENT) values (49,3021.02);
Insert into match_samples (ID,PAYMENT) values (50,3021.02);
Insert into match_samples (ID,PAYMENT) values (51,3021.02);
Insert into match_samples (ID,PAYMENT) values (52,3021.02);
Insert into match_samples (ID,PAYMENT) values (53,10207.94);
Insert into match_samples (ID,PAYMENT) values (54,10207.94);
Insert into match_samples (ID,PAYMENT) values (55,10207.94);
Insert into match_samples (ID,PAYMENT) values (56,10207.94);
Insert into match_samples (ID,PAYMENT) values (57,10207.94);
Insert into match_samples (ID,PAYMENT) values (58,10207.94);
Insert into match_samples (ID,PAYMENT) values (59,3080.42);
Insert into match_samples (ID,PAYMENT) values (60,3080.42);
Insert into match_samples (ID,PAYMENT) values (61,3080.42);
Insert into match_samples (ID,PAYMENT) values (62,3080.42);
Insert into match_samples (ID,PAYMENT) values (63,3080.42);
Insert into match_samples (ID,PAYMENT) values (64,3080.42);
Insert into match_samples (ID,PAYMENT) values (65,3080.42);
Insert into match_samples (ID,PAYMENT) values (66,3080.42);

Recursive SQL getting stuck
with samples (i, n) as (
select id, payment from match_samples
),
iterator (i, n, s, o) as (
select i, n, n, cast(n as varchar2(4000))
from samples smp
union all
select smp.i
, smp.n
, itr.s + smp.n
, itr.o || '~' || to_char(smp.n)
from iterator itr
join samples smp on smp.i > itr.i
where itr.s + smp.n <= 37027.73
)
select *
from iterator
where s = 37027.73;

Comments

Mike Kutz

bitmap
Bitmap indexes implies Serialization. That's like having the db parameter _SLOW=TRUE.
It can speed up SELECTS, but at a cost for other DML operations.
In my uses, it's not about the "low cardinality" of the answers in a column, but the "low cardinality" of your query results.
Eg finding out that a particular song, sung by Adam Sandler about Tinder results for other Eskimos in his tribe, is true
I've used them to speed up ad hoc queries on final reporting tables.
Billy uses them to show you can count 42B rows in under 1s.
Partition
"Prevent FTS" is one use for Partitions.
The primary use I've seen is for data management.
Need to remove 1 mo worth of data? Drop a Partition
Also, research ILM in the Data warehouse guide. This helps automate the task of moving chunks of old data for you. (Eg move 1mo worth of 3yr old data from tablespace on SSD to compressed read-only tablespace on SATA)

User_JNHXJ

Thank for replying mike
i have a couple of question
1.what u mean by ad hoc query is query using bind variable in where clause? or is there a different meaning?(from what i know ad hoc query is a type of query where result set is depent on the value supplied to a variable)

2."Need to remove 1 mo worth of data? Drop a Partition" even if we drop a partition the actual table still have the data from that partition right? and for what purpose we dropping this data? for memory or just deleting old data?

Mike Kutz

Ad hoc
This is what most end users want to perform.
APEX IR searches are ad hoc queries. Especially those faceted searches.
Bitmap indexes could drastically improve multi-column queries of these types even on a 1 B row table.
But, INSERT/UPDATE/DELETES would be horrible. You'll want to do infrequent bulk operations from a single transaction only. Tools like DBMS_PARALLEL_EXECUTE will cause the crud operation to take longer if parallel_level is higher than 1. (I learned this the hard way)
Drop Partition
You usually remove old data for legal reasons.
Partitions are individuals tables magically joined together to act as 1 table. If you read the history of Oracle, Partitions started in v7.3 as Partitioned VIEWs.
So, when you DROP a Partition, you are actually dropping a table.
Which then brings up Partition EXCHANGE. In this case you are actually swapping one table for another. And because the DD is only swapping the pointers, this is extremely fast.
You can swap in/out a table's worth of data in the blink of an eye. (I'm ignoring INDEXES)
Again, I find Partitions more useful for data management. But queries performance shouldn't be ignored.

User_JNHXJ

thanks for the partition part i got a little insigth about it.
but still have question about bitmap
can you explain example of "faceted searches" is it like result set with low cardinality?(FYI first time hear faceted word so i do not understand)

Mike Kutz
1 - 5

Post Details

Added on Oct 25 2021
37 comments
3,445 views