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

时间:2015-02-25 13:18:00

标签: sql oracle plsql subset-sum

我正在寻找你的专家的指导,找出一种方法来比较一个数字和一个数字子集的总和 喜欢

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)

1 个答案:

答案 0 :(得分:12)

你不需要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表中的少量值才能合理地解决它!

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