SQL Tutorial

SQL Clauses / Operators

SQL-Injection

SQL Functions

SQL Queries

PL/SQL

MySQL

SQL Server

Misc

Sum of digits equal to a given number in PL/SQL

If you're asking for a PL/SQL code that finds combinations of digits whose sum equals a specific target number, then the problem is a subset of the classic "subset sum" problem. The "subset sum" problem is a decision problem to determine if there exists a subset of the given set whose sum equals a specified sum.

Here's a PL/SQL code that finds and outputs combinations of digits (0 to 9) whose sum equals a specified target:

DECLARE
    target_sum NUMBER := 9; -- Desired sum
    combination VARCHAR2(100);
    temp_sum NUMBER;
    i NUMBER;
    j NUMBER;
    k NUMBER;
BEGIN
    -- Loop through combinations of three digits (can be modified for more digits)
    FOR i IN 0..9 LOOP
        FOR j IN 0..9 LOOP
            FOR k IN 0..9 LOOP
                temp_sum := i + j + k;
                IF temp_sum = target_sum THEN
                    combination := i || ', ' || j || ', ' || k;
                    DBMS_OUTPUT.PUT_LINE('Combination: ' || combination);
                END IF;
            END LOOP;
        END LOOP;
    END LOOP;
END;
/

In the code above, the program checks combinations of three digits (from 0 to 9) whose sum equals the target_sum. You can adjust the nested loops to consider more or fewer digits.

The example provided is for the purpose of demonstration and may not be the most optimized approach for larger sets or targets.

  1. PL/SQL Program to Find Sum of Digits Equal to a Given Number:

    DECLARE
        num INTEGER := 12345;
        target_sum INTEGER := 9; -- Set your target sum
        current_sum INTEGER := 0;
        temp_num INTEGER := num;
    BEGIN
        WHILE temp_num > 0 LOOP
            current_sum := current_sum + MOD(temp_num, 10);
            temp_num := temp_num / 10;
        END LOOP;
    
        IF current_sum = target_sum THEN
            DBMS_OUTPUT.PUT_LINE('Sum of digits is equal to the target.');
        ELSE
            DBMS_OUTPUT.PUT_LINE('Sum of digits is not equal to the target.');
        END IF;
    END;
    /
    
  2. Oracle SQL Function for Sum of Digits Matching a Target:

    CREATE OR REPLACE FUNCTION digit_sum_equals_target (
        num IN INTEGER,
        target_sum IN INTEGER
    ) RETURN VARCHAR2 IS
        current_sum INTEGER := 0;
        temp_num INTEGER := num;
    BEGIN
        WHILE temp_num > 0 LOOP
            current_sum := current_sum + MOD(temp_num, 10);
            temp_num := temp_num / 10;
        END LOOP;
    
        IF current_sum = target_sum THEN
            RETURN 'Sum of digits is equal to the target.';
        ELSE
            RETURN 'Sum of digits is not equal to the target.';
        END IF;
    END digit_sum_equals_target;
    /
    
  3. Finding Numbers with a Specific Digit Sum in PL/SQL:

    DECLARE
        target_sum INTEGER := 9; -- Set your target sum
    BEGIN
        FOR num_rec IN (SELECT num FROM your_table) LOOP
            -- Check digit sum equality
            IF digit_sum_equals_target(num_rec.num, target_sum) = 'Sum of digits is equal to the target.' THEN
                DBMS_OUTPUT.PUT_LINE('Number ' || num_rec.num || ' has the target digit sum.');
            END IF;
        END LOOP;
    END;
    /
    
  4. PL/SQL Procedure for Checking Digit Sum Equality:

    CREATE OR REPLACE PROCEDURE check_digit_sum_equality (
        num IN INTEGER,
        target_sum IN INTEGER
    ) IS
    BEGIN
        -- Check digit sum equality
        IF digit_sum_equals_target(num, target_sum) = 'Sum of digits is equal to the target.' THEN
            DBMS_OUTPUT.PUT_LINE('Number ' || num || ' has the target digit sum.');
        END IF;
    END check_digit_sum_equality;
    /
    
  5. Oracle SQL Query to Find Numbers with a Certain Digit Sum:

    SELECT num
    FROM your_table
    WHERE digit_sum_equals_target(num, 9) = 'Sum of digits is equal to the target';
    
  6. Oracle SQL Script to Identify Numbers with a Specific Digit Sum:

    DECLARE
        target_sum INTEGER := 9; -- Set your target sum
    BEGIN
        FOR num_rec IN (SELECT num FROM your_table) LOOP
            -- Check digit sum equality
            IF digit_sum_equals_target(num_rec.num, target_sum) = 'Sum of digits is equal to the target.' THEN
                DBMS_OUTPUT.PUT_LINE('Number ' || num_rec.num || ' has the target digit sum.');
            END IF;
        END LOOP;
    END;
    /
    
  7. PL/SQL Program to Filter Numbers by Digit Sum Equality:

    DECLARE
        target_sum INTEGER := 9; -- Set your target sum
    BEGIN
        FOR num_rec IN (SELECT num FROM your_table) LOOP
            -- Check digit sum equality
            IF digit_sum_equals_target(num_rec.num, target_sum) = 'Sum of digits is equal to the target.' THEN
                DBMS_OUTPUT.PUT_LINE('Number ' || num_rec.num || ' has the target digit sum.');
            END IF;
        END LOOP;
    END;
    /