Prevent Oracle minus statement from removing duplicates

前端 未结 7 1767
南方客
南方客 2021-02-15 19:39

Given these two tables:

CREATE TABLE TEST1 (TEST  VARCHAR2(1 BYTE))
CREATE TABLE TEST2 (TEST  VARCHAR2(1 BYTE))

Where TEST1 has two rows both w

7条回答
  •  滥情空心
    2021-02-15 20:02

    Another option:

    SELECT TEST, row_number() OVER (PARTITION BY TEST ORDER BY TEST) FROM TEST1
    MINUS
    SELECT TEST, row_number() OVER (PARTITION BY TEST ORDER BY TEST) FROM TEST2
    

    This would be MINUS with each duplicate treated as a distinct entry. Note in the example below, if TEST1 has two 'C' values and TEST2 has only one, you get one in the output.

    dev> select * from test1;
    
    T
    -
    A
    A
    B
    C
    C
    
    dev> select * from test2;
    
    T
    -
    B
    C
    
    dev>     SELECT TEST, row_number() OVER (PARTITION BY TEST ORDER BY TEST) FROM TEST1
      2      MINUS
      3      SELECT TEST, row_number() OVER (PARTITION BY TEST ORDER BY TEST) FROM TEST2
      4  /
    
    T ROW_NUMBER()OVER(PARTITIONBYTESTORDERBYTEST)
    - --------------------------------------------
    A                                            1
    A                                            2
    C                                            2
    

提交回复
热议问题