Oracle将框的尺寸与参考进行比较

时间:2018-03-29 09:49:45

标签: oracle

我的桌子的盒子尺寸为LENGTH,WIDTH和HEIGHT。我想将此框与参考框进行比较,以查看框是否大于参考框并输出每个框,如果框大或不大。

让我们说LENGTH,WIDTH和HEIGHT分别是4,6和2。并且参考盒尺寸是3,5和8.在这种情况下,盒子小于参考,因为(2,4,6)< (3,5,8)。

所以基本上我必须将有序框尺寸与有序给定尺寸进行比较。有没有办法用oracle做到这一点?

我已经遇到过最伟大和最少的功能,但我无法使用这些方法比较中间维度。

3 个答案:

答案 0 :(得分:1)

你可以这样做:

CREATE OR REPLACE TYPE NUMBER_TABLE_TYPE AS TABLE OF NUMBER;


DECLARE
    refBox NUMBER_TABLE_TYPE := NUMBER_TABLE_TYPE(4,6,2);
    inBox NUMBER_TABLE_TYPE := NUMBER_TABLE_TYPE(3,5,8);

    sortedRefBox NUMBER_TABLE_TYPE;
    sortedInBox NUMBER_TABLE_TYPE;

    fitting BOOLEAN := TRUE;

BEGIN

    IF refBox.count <> 3 OR inBox.count <> 3 THEN
        RAISE VALUE_ERROR;      
    END IF;

    SELECT COLUMN_VALUE 
    BULK COLLECT INTO sortedRefBox 
    FROM TABLE(refBox)
    ORDER BY COLUMN_VALUE;

    SELECT COLUMN_VALUE 
    BULK COLLECT INTO sortedinBox
    FROM TABLE(inBox)
    ORDER BY COLUMN_VALUE;

    FOR i IN 1..3 LOOP
        fitting := fitting AND sortedinBox(i) <= sortedRefBox(i);
    END LOOP;

    IF fitting THEN
        DBMS_OUTPUT.PUT_LINE('InBox fits in Reference Box' );
    else
        DBMS_OUTPUT.PUT_LINE('InBox does not fit in Reference Box' );   
    end if;

END;    

答案 1 :(得分:1)

中间值至少从三对中获得最大值:

-- sample data
with boxes(length, width, height) as (
  select 4, 6, 2 from dual union all
  select 2, 6, 4 from dual union all
  select 1, 3, 2 from dual union all
  select 9, 7, 8 from dual )
-- 
select length, width, height,
       case when least(least(length, width), height)           < 3
                 and least(greatest(length, width), 
                           greatest(length, height), 
                           greatest(width, height))            < 5
                 and greatest(greatest(length, width), height) < 8
           then 'smaller than reference'
           else 'bigger or equal'
       end comparison
  from boxes

结果:

    LENGTH      WIDTH     HEIGHT COMPARISON
---------- ---------- ---------- ----------------------
         4          6          2 smaller than reference
         2          6          4 smaller than reference
         1          3          2 smaller than reference
         9          7          8 bigger or equal

答案 2 :(得分:0)

我最终使用了GREATEST和LEAST函数,如下所示。

GREATEST(BOX_LENGTH, BOX_HEIGHT, BOX_WIDTH) > GREATEST(REF_LENGTH, REF_HEIGHT, REF_WIDTH) OR 

LEAST(BOX_LENGTH, BOX_HEIGHT, BOX_WIDTH) > LEAST(REF_LENGTH, REF_HEIGHT, REF_WIDTH) OR 

BOX_LENGTH + BOX_HEIGHT + BOX_WIDTH - GREATEST(BOX_LENGTH, BOX_HEIGHT, BOX_WIDTH) - LEAST(BOX_LENGTH, BOX_HEIGHT, BOX_WIDTH) > REF_LENGTH + REF_HEIGHT + REF_WIDTH - GREATEST(REF_LENGTH, REF_HEIGHT, REF_WIDTH) - LEAST(REF_LENGTH, REF_HEIGHT, REF_WIDTH) 

那是

(Sum of dimensions) - Greatest - Least = Middle dimension