gpt4 book ai didi

sql - 将数字与数字子集的总和进行比较

转载 作者:行者123 更新时间:2023-12-04 14:06:23 26 4
gpt4 key购买 nike

我正在寻找您的大师的指导,以找出一种将数字与数字子集的总和进行比较的方法
喜欢

DECLARE
L_NUM_TO_COMPARE NUMBER := 0;
L_NUM_SUBSET NUMBER := 0;
BEGIN
FOR MAIN_REC IN (
SELECT 1 ID, 25150 ASSIGN_AMT FROM DUAL
UNION ALL
SELECT 2 ID, 19800 ASSIGN_AMT FROM DUAL
UNION ALL
SELECT 3 ID, 27511 ASSIGN_AMT FROM DUAL
) LOOP
L_NUM_TO_COMPARE := MAIN_REC.ASSIGN_AMT;
DBMS_OUTPUT.PUT_LINE( L_NUM_TO_COMPARE);

FOR C IN (
SELECT 1 ID, 7120 WORK_AMT FROM DUAL
UNION ALL SELECT 2 ID, 8150 WORK_AMT FROM DUAL
UNION ALL SELECT 3 ID, 8255 WORK_AMT FROM DUAL
UNION ALL SELECT 4 ID, 9051 WORK_AMT FROM DUAL
UNION ALL SELECT 5 ID, 1220 WORK_AMT FROM DUAL
UNION ALL SELECT 6 ID, 12515 WORK_AMT FROM DUAL
UNION ALL SELECT 7 ID, 13555 WORK_AMT FROM DUAL
UNION ALL SELECT 8 ID, 5221 WORK_AMT FROM DUAL
UNION ALL SELECT 9 ID, 812 WORK_AMT FROM DUAL
UNION ALL SELECT 10 ID, 6562 WORK_AMT FROM DUAL
ORDER BY 2 DESC
) LOOP
L_NUM_SUBSET := NVL(L_NUM_SUBSET,0) + C.WORK_AMT;
DBMS_OUTPUT.PUT_LINE( L_NUM_SUBSET);
/*
I NEED TO PUT SOME LOGIC HOW CAN I FIND NEAREST SUM OF SUBSET
*/
IF MAIN_REC.ASSIGN_AMT = L_NUM_SUBSET THEN
DBMS_OUTPUT.PUT_LINE( L_NUM_SUBSET);
END IF;
END LOOP;
END LOOP;
END;

我在这个论坛上搜索过,发现了一个问题
Sum of Sub set of numbers

这几乎与我的要求相同,我需要什么可以指出我如何在 PL/SQL 中执行此操作
我有(Oracle DB 11g R2)

最佳答案

您不需要 PL/SQL 来解决这个问题。这是一个非常有趣的问题,需要单独使用 SQL 来解决,I've written up a blog post to explain my answer in more detail .

您提出问题的方式,我假设您并没有真正解决 subset sum problem ,但这是一个更简单的问题,您想将一个数字与一组非常有限的子集进行比较,即按 WORK_AMT 排序的那些子集。上升,没有间隙。

简化问题

这可以单独使用 Oracle SQL 解决:

WITH
ASSIGN(ID, ASSIGN_AMT) AS (
SELECT 1, 25150 FROM DUAL
UNION ALL SELECT 2, 19800 FROM DUAL
UNION ALL SELECT 3, 27511 FROM DUAL
),
VALS (ID, WORK_AMT) AS (
SELECT 1 , 7120 FROM DUAL
UNION ALL SELECT 2 , 8150 FROM DUAL
UNION ALL SELECT 3 , 8255 FROM DUAL
UNION ALL SELECT 4 , 9051 FROM DUAL
UNION ALL SELECT 5 , 1220 FROM DUAL
UNION ALL SELECT 6 , 12515 FROM DUAL
UNION ALL SELECT 7 , 13555 FROM DUAL
UNION ALL SELECT 8 , 5221 FROM DUAL
UNION ALL SELECT 9 , 812 FROM DUAL
UNION ALL SELECT 10, 6562 FROM DUAL
),
SUMS (ID, WORK_AMT, SUBSET_SUM) AS (
SELECT VALS.*, SUM (WORK_AMT) OVER (ORDER BY ID)
FROM VALS
)
SELECT
ASSIGN.ID,
ASSIGN.ASSIGN_AMT,
MIN (SUBSET_SUM) KEEP (
DENSE_RANK FIRST
ORDER BY ABS (ASSIGN_AMT - SUBSET_SUM)
) AS CLOSEST_SUM
FROM
ASSIGN
CROSS JOIN
SUMS
GROUP BY
ASSIGN.ID, ASSIGN.ASSIGN_AMT

以上产生:

ID  ASSIGN_AMT  CLOSEST_SUM
---------------------------
1 25150 29085
2 19800 20935
3 27511 29085

实际的子集求和问题

请注意,这个问题在时间和空间上具有指数级的复杂性。只能对 WORK中的少量值合理求解 table !
WITH
ASSIGN (ID, ASSIGN_AMT) AS (
SELECT 1, 25150 FROM DUAL
UNION ALL SELECT 2, 19800 FROM DUAL
UNION ALL SELECT 3, 27511 FROM DUAL
),
WORK (ID, WORK_AMT) AS (
SELECT 1 , 7120 FROM DUAL
UNION ALL SELECT 2 , 8150 FROM DUAL
UNION ALL SELECT 3 , 8255 FROM DUAL
UNION ALL SELECT 4 , 9051 FROM DUAL
UNION ALL SELECT 5 , 1220 FROM DUAL
UNION ALL SELECT 6 , 12515 FROM DUAL
UNION ALL SELECT 7 , 13555 FROM DUAL
UNION ALL SELECT 8 , 5221 FROM DUAL
UNION ALL SELECT 9 , 812 FROM DUAL
UNION ALL SELECT 10, 6562 FROM DUAL
),
SUMS (SUBSET_SUM, MAX_ID) AS (
SELECT WORK_AMT, ID FROM WORK
UNION ALL
SELECT WORK_AMT + SUBSET_SUM, GREATEST (MAX_ID, WORK.ID)
FROM SUMS JOIN WORK
ON SUMS.MAX_ID < WORK.ID
)
SELECT
ASSIGN.ID,
ASSIGN.ASSIGN_AMT,
MIN (SUBSET_SUM) KEEP (
DENSE_RANK FIRST
ORDER BY ABS (ASSIGN_AMT - SUBSET_SUM)
) AS CLOSEST_SUM
FROM SUMS
CROSS JOIN ASSIGN
GROUP BY ASSIGN.ID, ASSIGN.ASSIGN_AMT

这现在产生:

ID  ASSIGN_AMT  CLOSEST_SUM
---------------------------
1 25150 25133
2 19800 19768
3 27511 27488

关于sql - 将数字与数字子集的总和进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28720029/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com