满足条件时,仅消除SUM为0的值

时间:2015-08-13 13:50:51

标签: sql oracle

我目前有一个完美的查询,结果都是正确的,但是需要修改以删除具有特定VOT和匹配VONO的未使用的行。

目前查询会返回这样的结果集。

样本1

PART_NO TRANSACTION YEAR    QUANTITY    INV_COST    PARTSDESCRIPTION    COMPANY VONO            TEXT        YEAR_PERIOD_KEY ACCOUNT ACC         CODE_B  CODE_B_DESC CODE_C  CODE_C_DESC CODE_F  CODE_F_DESC REFERENCE_NUMBER    VOT     VOTD        AMOUNT  SUPPLIER_ID NAME        LINE_NO OBJECT_CLASS    FOCDESCRIPTION  ROW_NO   
10438   Issue       2015    1           12.39       coat                19      2015000314      Issue       201502          5635    Equipment   426     Floor       16      Operations  null    null        2869                MPL     11-FEB-15   12.39   null        null        null    null            null            2    
null    null        2015    null        null        null                19      2015000314      Automotive  201501          5513    Maintenance 426     Floor       16      Operations  80016   Mobile      MI074755            J       07-JAN-15   77.82   52254       Automotive  null    131100          Mobile          2    
null    Reg         2015    1           76.5        hose                19      2015000314      Register    201502          5490    consumable  426     Floor       16      Operations  E80009  Group       P42085              0       03-FEB-15   76.5    null        null        1       132300          Group           8    
null    null        2015    null        null        null                19      2015000314      Automotive  201501          5513    Maintenance 426     Floor       16      Operations  80016   Mobile      MI074755            J       07-JAN-15   -77.82  52254       Automotive  null    131100          Mobile          4    
null    Reg         2015    4           86.53       battery             19      2015000314      Register    201502          5430    Material    426     Floor       16      Operations  E80005  Running     P42092              0       03-FEB-15   346.12  null        null        1       142100          Runner          10
null    null        2015    null        null        null                19      2015000314      Automotive  201501          5513    Maintenance 426     Floor       16      Operations  80016   Mobile      MI074755            J       07-JAN-15   62.44   52254       Automotive  null    131100          Mobile          4    

现在我想要做的只是当VOT = J和VONO相同且金额相互抵消时我希望它们不被显示(在这种情况下,两个值-77.82和77.82取消,因为它们是VOT = J,它们都是同一个VONO。

这将产生如下所需的输出:

PART_NO TRANSACTION YEAR    QUANTITY    INV_COST    PARTSDESCRIPTION    COMPANY VONO            TEXT        YEAR_PERIOD_KEY ACCOUNT ACC         CODE_B  CODE_B_DESC CODE_C  CODE_C_DESC CODE_F  CODE_F_DESC REFERENCE_NUMBER    VOT     VOTD        AMOUNT  SUPPLIER_ID NAME        LINE_NO OBJECT_CLASS    FOCDESCRIPTION  ROW_NO   
10438   Issue       2015    1           12.39       coat                19      2015000314      Issue       201502          5635    Equipment   426     Floor       16      Operations  null    null        2869                MPL     11-FEB-15   12.39   null        null        null    null            null            2    
null    Reg         2015    1           76.5        hose                19      2015000314      Register    201502          5490    consumable  426     Floor       16      Operations  E80009  Group       P42085              0       03-FEB-15   76.5    null        null        1       132300          Group           8    
null    Reg         2015    4           86.53       battery             19      2015000314      Register    201502          5430    Material    426     Floor       16      Operations  E80005  Running     P42092              0       03-FEB-15   346.12  null        null        1       142100          Runner          10
null    null        2015    null        null        null                19      2015000314      Automotive  201501          5513    Maintenance 426     Floor       16      Operations  80016   Mobile      MI074755            J       07-JAN-15   62.44   52254       Automotive  null    131100          Mobile          4    

目前查询是这样,并给出第一个输出。

SELECT DISTINCT  pt.part_no, pt.TRANSACTION, g.year, pt.quantity, 
(g.amount/pt.quantity) as Inv_Cost, 
case when pc.description is null then p.description else pc.description end as partsdescription, 
g.company, g.vono, g.text, g.year_period_key, g.acc, g.code_b, g.code_b_desc, g.code_c,
g.code_c_desc, g.code_f, g.code_f_desc, g.reference_number, g.vot, g.votd, g.amount, 
s.supplier_id, s.NAME, p.line_no, foc.object_class, foc.description AS focdescription, 
g.row_no
FROM gen g
LEFT OUTER JOIN phist pt ON g.accid = pt.accid  
FULL OUTER JOIN partc pc ON pt.part_no = pc.part_no
LEFT OUTER JOIN pola p ON pt.order_no = p.order_no AND pt.rel = p.line_no 
LEFT OUTER JOIN sia s ON g.party_type_id = s.supplier_id
LEFT OUTER JOIN cust c ON g.party_type_id = c.customer_id 
LEFT OUTER JOIN inv d ON g.COMPANY = d.COMPANY AND g.vono = d.vonore AND g.vot = d.VOUCHER_TYPE_REF
LEFT OUTER JOIN III b ON d.company = b.company AND d.invid = b.invid 
LEFT OUTER JOIN ob fo ON g.code_f = fo.object_id
LEFT OUTER JOIN obcl foc ON fo.object_class = foc.object_class
WHERE g.year = '2015' and g.company = '19'
AND (g.acc > '2999' OR g.acc IN ('1401','1450'))
AND g.code_b IN  ('426', '028', '124', '125') 
AND g.vot != 'MPL'

更新(XQbert)

好的,这是一个(BASIC)SQL小提琴:http://sqlfiddle.com/#!4/79611a/1/0

正在发生的是它正在删除所有VOT = J而不是每个VONO总共为0的那些。

示例数据:

PART_NO  TRANSACTION    YEAR        QUANTITY    INV_COST    PARTSDESCRIPTION    COMPANY VONO         TEXT       YEAR_PERIOD_KEY ACCOUNT  ACC            CODE_B  CODE_B_DESC CODE_C  CODE_C_DESC  CODE_F  CODE_F_DESC    REFERENCE_NUMBER    VOT  VOTD       AMOUNT   SUPPLIER_ID    NAME        LINE_NO  OBJECT_CLASS   FOCDESCRIPTION  ROW_NO 
10438    Issue          2015        1           12.39       coat                19      2015000314   Issue      201502          5635     Equipment      426     Floor       16      Operations   null    null           2869                MPL  11-FEB-15  12.39    null           null        null     null           null            2
null     null           2015        null        null        null                19      2015000314   Automotive 201501          5513     Maintenance    426     Floor       16      Operations   80016   Mobile         MI074755            J    07-JAN-15  77.82    52254          Automotive  null     131100         Mobile          2
null     Reg            2015        1           76.5        hose                19      2015000314   Register   201502          5490     consumable     426     Floor       16      Operations   E80009  Group          P42085              0    03-FEB-15  76.5     null           null        1        132300         Group           8
null     null           2015        null        null        null                19      2015000314   Automotive 201501          5513     Maintenance    426     Floor       16      Operations   80016   Mobile         MI074755            J    07-JAN-15  -77.82   52254          Automotive  null     131100         Mobile          4
null     Reg            2015        4           86.53       battery             19      2015000314   Register   201502          5430     Material       426     Floor       16      Operations   E80005  Running        P42092              0    03-FEB-15  346.12   null           null        1        142100         Runner          10
null     null           2015        null        null        null                19      2015000314   Automotive 201501          5513     Maintenance    426     Floor       16      Operations   80016   Mobile         MI074755            J    07-JAN-15  62.44    52254          Automotive  null     131100         Mobile          4
null     null           2015        null        null        null                19      2015000999   Bike       201507          5699     Parts          426     Floor       16      Operations   89999   fridge         MA864654            J    07-JUL-15  899.84   52254          Bike        null     65222          fridge          4

输出:

PART_NO  TRANSACTION    YEAR        QUANTITY    INV_COST    PARTSDESCRIPTION    COMPANY VONO         TEXT       YEAR_PERIOD_KEY ACCOUNT  ACC            CODE_B  CODE_B_DESC CODE_C  CODE_C_DESC  CODE_F  CODE_F_DESC    REFERENCE_NUMBER    VOT  VOTD       AMOUNT   SUPPLIER_ID    NAME        LINE_NO  OBJECT_CLASS   FOCDESCRIPTION  ROW_NO 
10438    Issue          2015        1           12.39       coat                19      2015000314   Issue      201502          5635     Equipment      426     Floor       16      Operations   null    null           2869                MPL  11-FEB-15  12.39    null           null        null     null           null            2
null     Reg            2015        1           76.5        hose                19      2015000314   Register   201502          5490     consumable     426     Floor       16      Operations   E80009  Group          P42085              0    03-FEB-15  76.5     null           null        1        132300         Group           8
null     Reg            2015        4           86.53       battery             19      2015000314   Register   201502          5430     Material       426     Floor       16      Operations   E80005  Running        P42092              0    03-FEB-15  346.12   null           null        1        142100         Runner          10

预期产出:

PART_NO  TRANSACTION    YEAR        QUANTITY    INV_COST    PARTSDESCRIPTION    COMPANY VONO         TEXT       YEAR_PERIOD_KEY ACCOUNT  ACC            CODE_B  CODE_B_DESC CODE_C  CODE_C_DESC  CODE_F  CODE_F_DESC    REFERENCE_NUMBER    VOT  VOTD       AMOUNT   SUPPLIER_ID    NAME        LINE_NO  OBJECT_CLASS   FOCDESCRIPTION  ROW_NO 
10438    Issue          2015        1           12.39       coat                19      2015000314   Issue      201502          5635     Equipment      426     Floor       16      Operations   null    null           2869                MPL  11-FEB-15  12.39    null           null        null     null           null            2
null     Reg            2015        1           76.5        hose                19      2015000314   Register   201502          5490     consumable     426     Floor       16      Operations   E80009  Group          P42085              0    03-FEB-15  76.5     null           null        1        132300         Group           8
null     Reg            2015        4           86.53       battery             19      2015000314   Register   201502          5430     Material       426     Floor       16      Operations   E80005  Running        P42092              0    03-FEB-15  346.12   null           null        1        142100         Runner          10
null     null           2015        null        null        null                19      2015000314   Automotive 201501          5513     Maintenance    426     Floor       16      Operations   80016   Mobile         MI074755            J    07-JAN-15  62.44    52254          Automotive  null     131100         Mobile          4
null     null           2015        null        null        null                19      2015000999   Bike       201507          5699     Parts          426     Floor       16      Operations   89999   fridge         MA864654            J    07-JUL-15  899.84   52254          Bike        null     65222          fridge          4

2 个答案:

答案 0 :(得分:2)

未经测试,因为样本数据太复杂了......

这仅仅建立在你所做的事情之上。可能有一种更简单的方式。

这样做是在VoNo上生成一个公用表表达式(CTE)并将一个CTE连接到另一个CTE,并确保两个表都有一个J的Vot,然后如果表中的一个数量与另一个表相反则他们匹配,它消除了它们。 这会产生一个问题但是如果你有两个以上相同VoNo的金额,例如额外的-75金额。它也将被排除在外......也许不是所希望的。但是,如果数据无法以这种方式发生......没问题。

初步回复:

With CTE AS (
SELECT DISTINCT  pt.part_no, pt.TRANSACTION, g.year, pt.quantity, 
(g.amount/pt.quantity) as Inv_Cost, 
case when pc.description is null then p.description else pc.description end as partsdescription, 
g.company, g.vono, g.text, g.year_period_key, g.acc, g.code_b, g.code_b_desc, g.code_c,
g.code_c_desc, g.code_f, g.code_f_desc, g.reference_number, g.vot, g.votd, g.amount, 
s.supplier_id, s.NAME, p.line_no, foc.object_class, foc.description AS focdescription, 
g.row_no
FROM gen g
LEFT OUTER JOIN phist pt ON g.accid = pt.accid  
FULL OUTER JOIN partc pc ON pt.part_no = pc.part_no
LEFT OUTER JOIN pola p ON pt.order_no = p.order_no AND pt.rel = p.line_no 
LEFT OUTER JOIN sia s ON g.party_type_id = s.supplier_id
LEFT OUTER JOIN cust c ON g.party_type_id = c.customer_id 
LEFT OUTER JOIN inv d ON g.COMPANY = d.COMPANY AND g.vono = d.vonore AND g.vot = d.VOUCHER_TYPE_REF
LEFT OUTER JOIN III b ON d.company = b.company AND d.invid = b.invid 
LEFT OUTER JOIN ob fo ON g.code_f = fo.object_id
LEFT OUTER JOIN obcl foc ON fo.object_class = foc.object_class
WHERE g.year = '2015' and g.company = '19'
AND (g.acc > '2999' OR g.acc IN ('1401','1450'))
AND g.code_b IN  ('426', '028', '124', '125') 
AND g.vot != 'MPL')

SELECT  * from CTE MINUS
SELECT A.* 
FROM CTE A
INNER JOIN CTE B
 on A.VoNo=B.VoNo
WHERE A.Amount+B.Amount = 0 
and A.Vot='J' and B.Vot='J'

添加联合(放入联合然后cte联合结果将不起作用.CTE必须是第一个,UNION必须在生成的CTE之后,但它可能在第一个选择之前或之后。

With CTE AS (
SELECT DISTINCT  pt.part_no, pt.TRANSACTION, g.year, pt.quantity, 
(g.amount/pt.quantity) as Inv_Cost, 
case when pc.description is null then p.description else pc.description end as partsdescription, 
g.company, g.vono, g.text, g.year_period_key, g.acc, g.code_b, g.code_b_desc, g.code_c,
g.code_c_desc, g.code_f, g.code_f_desc, g.reference_number, g.vot, g.votd, g.amount, 
s.supplier_id, s.NAME, p.line_no, foc.object_class, foc.description AS focdescription, 
g.row_no
FROM gen g
LEFT OUTER JOIN phist pt ON g.accid = pt.accid  
FULL OUTER JOIN partc pc ON pt.part_no = pc.part_no
LEFT OUTER JOIN pola p ON pt.order_no = p.order_no AND pt.rel = p.line_no 
LEFT OUTER JOIN sia s ON g.party_type_id = s.supplier_id
LEFT OUTER JOIN cust c ON g.party_type_id = c.customer_id 
LEFT OUTER JOIN inv d ON g.COMPANY = d.COMPANY AND g.vono = d.vonore AND g.vot = d.VOUCHER_TYPE_REF
LEFT OUTER JOIN III b ON d.company = b.company AND d.invid = b.invid 
LEFT OUTER JOIN ob fo ON g.code_f = fo.object_id
LEFT OUTER JOIN obcl foc ON fo.object_class = foc.object_class
WHERE g.year = '2015' and g.company = '19'
AND (g.acc > '2999' OR g.acc IN ('1401','1450'))
AND g.code_b IN  ('426', '028', '124', '125') 
AND g.vot != 'MPL')
--Could put your SELECT here
--UNION ALL
SELECT  * from CTE MINUS
SELECT A.* 
FROM CTE A
INNER JOIN CTE B
 on A.VoNo=B.VoNo
WHERE A.Amount+B.Amount = 0 
and A.Vot='J' and B.Vot='J'
--UNION ALL or here...
--YOUR UNION

答案 1 :(得分:2)

SQL Fiddle

Oracle 11g R2架构设置

我为你的数据添加了3行 - 两行有+100金额,一行有-100金额,表明这可以处理不等数量的正负行。 (还将AMOUNT列更改为NUMBER(10,2))。

CREATE TABLE gen
    ("PART_NO" varchar2(5), "TRANSACTION" varchar2(5), "YEAR" int, "QUANTITY" varchar2(4), "INV_COST" varchar2(5), "PARTSDESCRIPTION" varchar2(7), "COMPANY" int, "VONO" int, "TEXT" varchar2(10), "YEAR_PERIOD_KEY" int, "ACCOUNT" int, "ACC" varchar2(11), "CODE_B" int, "CODE_B_DESC" varchar2(5), "CODE_C" int, "CODE_C_DESC" varchar2(10), "CODE_F" varchar2(6), "CODE_F_DESC" varchar2(7), "REFERENCE_NUMBER" varchar2(8), "VOT" varchar2(3), "VOTD" varchar2(30), "AMOUNT" NUMBER(10,2), "SUPPLIER_ID" varchar2(5), "NAME" varchar2(10), "LINE_NO" varchar2(4), "OBJECT_CLASS" varchar2(6), "FOCDESCRIPTION" varchar2(6), "ROW_NO" int)
;

INSERT ALL 
    INTO gen ("PART_NO", "TRANSACTION", "YEAR", "QUANTITY", "INV_COST", "PARTSDESCRIPTION", "COMPANY", "VONO", "TEXT", "YEAR_PERIOD_KEY", "ACCOUNT", "ACC", "CODE_B", "CODE_B_DESC", "CODE_C", "CODE_C_DESC", "CODE_F", "CODE_F_DESC", "REFERENCE_NUMBER", "VOT", "VOTD", "AMOUNT", "SUPPLIER_ID", "NAME", "LINE_NO", "OBJECT_CLASS", "FOCDESCRIPTION", "ROW_NO")
         VALUES ('10438', 'Issue', 2015, '1', '12.39', 'coat', 19, 2015000314, 'Issue', 201502, 5635, 'Equipment', 426, 'Floor', 16, 'Operations', NULL, NULL, '2869', 'MPL', '11-Feb-2015 12:00:00 AM', 12.39, NULL, NULL, NULL, NULL, NULL, 2)
    INTO gen ("PART_NO", "TRANSACTION", "YEAR", "QUANTITY", "INV_COST", "PARTSDESCRIPTION", "COMPANY", "VONO", "TEXT", "YEAR_PERIOD_KEY", "ACCOUNT", "ACC", "CODE_B", "CODE_B_DESC", "CODE_C", "CODE_C_DESC", "CODE_F", "CODE_F_DESC", "REFERENCE_NUMBER", "VOT", "VOTD", "AMOUNT", "SUPPLIER_ID", "NAME", "LINE_NO", "OBJECT_CLASS", "FOCDESCRIPTION", "ROW_NO")
         VALUES (NULL, NULL, 2015, NULL, NULL, NULL, 19, 2015000314, 'Automotive', 201501, 5513, 'Maintenance', 426, 'Floor', 16, 'Operations', '80016', 'Mobile', 'MI074755', 'J', '07-Jan-2015 12:00:00 AM', 77.82, '52254', 'Automotive', NULL, '131100', 'Mobile', 2)
    INTO gen ("PART_NO", "TRANSACTION", "YEAR", "QUANTITY", "INV_COST", "PARTSDESCRIPTION", "COMPANY", "VONO", "TEXT", "YEAR_PERIOD_KEY", "ACCOUNT", "ACC", "CODE_B", "CODE_B_DESC", "CODE_C", "CODE_C_DESC", "CODE_F", "CODE_F_DESC", "REFERENCE_NUMBER", "VOT", "VOTD", "AMOUNT", "SUPPLIER_ID", "NAME", "LINE_NO", "OBJECT_CLASS", "FOCDESCRIPTION", "ROW_NO")
         VALUES (NULL, 'Reg', 2015, '1', '76.5', 'hose', 19, 2015000314, 'Register', 201502, 5490, 'consumable', 426, 'Floor', 16, 'Operations', 'E80009', 'Group', 'P42085', '0', '03-Feb-2015 12:00:00 AM', 76.5, NULL, NULL, '1', '132300', 'Group', 8)
    INTO gen ("PART_NO", "TRANSACTION", "YEAR", "QUANTITY", "INV_COST", "PARTSDESCRIPTION", "COMPANY", "VONO", "TEXT", "YEAR_PERIOD_KEY", "ACCOUNT", "ACC", "CODE_B", "CODE_B_DESC", "CODE_C", "CODE_C_DESC", "CODE_F", "CODE_F_DESC", "REFERENCE_NUMBER", "VOT", "VOTD", "AMOUNT", "SUPPLIER_ID", "NAME", "LINE_NO", "OBJECT_CLASS", "FOCDESCRIPTION", "ROW_NO")
         VALUES (NULL, NULL, 2015, NULL, NULL, NULL, 19, 2015000314, 'Automotive', 201501, 5513, 'Maintenance', 426, 'Floor', 16, 'Operations', '80016', 'Mobile', 'MI074755', 'J', '07-Jan-2015 12:00:00 AM', -77.82, '52254', 'Automotive', NULL, '131100', 'Mobile', 4)
    INTO gen ("PART_NO", "TRANSACTION", "YEAR", "QUANTITY", "INV_COST", "PARTSDESCRIPTION", "COMPANY", "VONO", "TEXT", "YEAR_PERIOD_KEY", "ACCOUNT", "ACC", "CODE_B", "CODE_B_DESC", "CODE_C", "CODE_C_DESC", "CODE_F", "CODE_F_DESC", "REFERENCE_NUMBER", "VOT", "VOTD", "AMOUNT", "SUPPLIER_ID", "NAME", "LINE_NO", "OBJECT_CLASS", "FOCDESCRIPTION", "ROW_NO")
         VALUES (NULL, 'Reg', 2015, '4', '86.53', 'battery', 19, 2015000314, 'Register', 201502, 5430, 'Material', 426, 'Floor', 16, 'Operations', 'E80005', 'Running', 'P42092', '0', '03-Feb-2015 12:00:00 AM', 346.12, NULL, NULL, '1', '142100', 'Runner', 10)
    INTO gen ("PART_NO", "TRANSACTION", "YEAR", "QUANTITY", "INV_COST", "PARTSDESCRIPTION", "COMPANY", "VONO", "TEXT", "YEAR_PERIOD_KEY", "ACCOUNT", "ACC", "CODE_B", "CODE_B_DESC", "CODE_C", "CODE_C_DESC", "CODE_F", "CODE_F_DESC", "REFERENCE_NUMBER", "VOT", "VOTD", "AMOUNT", "SUPPLIER_ID", "NAME", "LINE_NO", "OBJECT_CLASS", "FOCDESCRIPTION", "ROW_NO")
         VALUES (NULL, NULL, 2015, NULL, NULL, NULL, 19, 2015000314, 'Automotive', 201501, 5513, 'Maintenance', 426, 'Floor', 16, 'Operations', '80016', 'Mobile', 'MI074755', 'J', '07-Jan-2015 12:00:00 AM', 62.44, '52254', 'Automotive', NULL, '131100', 'Mobile', 4)
    INTO gen ("PART_NO", "TRANSACTION", "YEAR", "QUANTITY", "INV_COST", "PARTSDESCRIPTION", "COMPANY", "VONO", "TEXT", "YEAR_PERIOD_KEY", "ACCOUNT", "ACC", "CODE_B", "CODE_B_DESC", "CODE_C", "CODE_C_DESC", "CODE_F", "CODE_F_DESC", "REFERENCE_NUMBER", "VOT", "VOTD", "AMOUNT", "SUPPLIER_ID", "NAME", "LINE_NO", "OBJECT_CLASS", "FOCDESCRIPTION", "ROW_NO")
         VALUES (NULL, NULL, 2015, NULL, NULL, NULL, 19, 2015000999, 'Bike', 201507, 5699, 'Parts', 426, 'Floor', 16, 'Operations', '89999', 'fridge', 'MA864654', 'J', '07-Jul-2015 12:00:00 AM', 899.84, '52254', 'Bike', NULL, '65222', 'fridge', 4)
    INTO gen ("PART_NO", "TRANSACTION", "YEAR", "QUANTITY", "INV_COST", "PARTSDESCRIPTION", "COMPANY", "VONO", "TEXT", "YEAR_PERIOD_KEY", "ACCOUNT", "ACC", "CODE_B", "CODE_B_DESC", "CODE_C", "CODE_C_DESC", "CODE_F", "CODE_F_DESC", "REFERENCE_NUMBER", "VOT", "VOTD", "AMOUNT", "SUPPLIER_ID", "NAME", "LINE_NO", "OBJECT_CLASS", "FOCDESCRIPTION", "ROW_NO")
         VALUES (NULL, NULL, 2015, NULL, NULL, NULL, 19, 2015000314, 'Automotive', 201501, 5513, 'Maintenance', 426, 'Floor', 16, 'Operations', '80016', 'Mobile', 'MI074755', 'J', '07-Jan-2015 12:00:00 AM', 100.00, '52254', 'Automotive', NULL, '131100', 'Mobile', 4)
    INTO gen ("PART_NO", "TRANSACTION", "YEAR", "QUANTITY", "INV_COST", "PARTSDESCRIPTION", "COMPANY", "VONO", "TEXT", "YEAR_PERIOD_KEY", "ACCOUNT", "ACC", "CODE_B", "CODE_B_DESC", "CODE_C", "CODE_C_DESC", "CODE_F", "CODE_F_DESC", "REFERENCE_NUMBER", "VOT", "VOTD", "AMOUNT", "SUPPLIER_ID", "NAME", "LINE_NO", "OBJECT_CLASS", "FOCDESCRIPTION", "ROW_NO")
         VALUES (NULL, NULL, 2015, NULL, NULL, NULL, 19, 2015000314, 'Automotive', 201501, 5513, 'Maintenance', 426, 'Floor', 16, 'Operations', '80016', 'Mobile', 'MI074755', 'J', '07-Jan-2015 12:00:00 AM', 100.00, '52254', 'Automotive', NULL, '131100', 'Mobile', 4)
    INTO gen ("PART_NO", "TRANSACTION", "YEAR", "QUANTITY", "INV_COST", "PARTSDESCRIPTION", "COMPANY", "VONO", "TEXT", "YEAR_PERIOD_KEY", "ACCOUNT", "ACC", "CODE_B", "CODE_B_DESC", "CODE_C", "CODE_C_DESC", "CODE_F", "CODE_F_DESC", "REFERENCE_NUMBER", "VOT", "VOTD", "AMOUNT", "SUPPLIER_ID", "NAME", "LINE_NO", "OBJECT_CLASS", "FOCDESCRIPTION", "ROW_NO")
         VALUES (NULL, NULL, 2015, NULL, NULL, NULL, 19, 2015000314, 'Automotive', 201501, 5513, 'Maintenance', 426, 'Floor', 16, 'Operations', '80016', 'Mobile', 'MI074755', 'J', '07-Jan-2015 12:00:00 AM', -100.00, '52254', 'Automotive', NULL, '131100', 'Mobile', 4)
SELECT * FROM dual
;

查询1

With UnindexedValues AS (
  SELECT * FROM gen
),
IndexedValues AS (
  SELECT v.*,
         ROW_NUMBER() OVER ( PARTITION BY VONO, VOT, AMOUNT ORDER BY VOTD ) AS AmtIdx
  FROM   UnindexedValues v
)
SELECT PART_NO, TRANSACTION, YEAR,
       QUANTITY, INV_COST, PARTSDESCRIPTION,
       COMPANY, VONO, TEXT,
       YEAR_PERIOD_KEY, ACCOUNT, ACC,
       CODE_B, CODE_B_DESC, CODE_C,
       CODE_C_DESC, CODE_F, CODE_F_DESC,
       REFERENCE_NUMBER, VOT, VOTD,
       AMOUNT, SUPPLIER_ID, NAME,
       LINE_NO, OBJECT_CLASS, FOCDESCRIPTION,
       ROW_NO
FROM   IndexedValues i
WHERE  NOT EXISTS (
  SELECT 'X'
  FROM   IndexedValues x
  WHERE  i.VoNo   = x.VoNo
  AND    i.Vot    = x.Vot
  AND    i.Vot    = 'J'
  AND    i.Amount = -x.Amount
  AND    i.AmtIdx = x.AmtIdx
)

<强> Results

| PART_NO | TRANSACTION | YEAR | QUANTITY | INV_COST | PARTSDESCRIPTION | COMPANY |       VONO |       TEXT | YEAR_PERIOD_KEY | ACCOUNT |         ACC | CODE_B | CODE_B_DESC | CODE_C | CODE_C_DESC | CODE_F | CODE_F_DESC | REFERENCE_NUMBER | VOT |                    VOTD | AMOUNT | SUPPLIER_ID |       NAME | LINE_NO | OBJECT_CLASS | FOCDESCRIPTION | ROW_NO |
|---------|-------------|------|----------|----------|------------------|---------|------------|------------|-----------------|---------|-------------|--------|-------------|--------|-------------|--------|-------------|------------------|-----|-------------------------|--------|-------------|------------|---------|--------------|----------------|--------|
|  (null) |         Reg | 2015 |        1 |     76.5 |             hose |      19 | 2015000314 |   Register |          201502 |    5490 |  consumable |    426 |       Floor |     16 |  Operations | E80009 |       Group |           P42085 |   0 | 03-Feb-2015 12:00:00 AM |   76.5 |      (null) |     (null) |       1 |       132300 |          Group |      8 |
|   10438 |       Issue | 2015 |        1 |    12.39 |             coat |      19 | 2015000314 |      Issue |          201502 |    5635 |   Equipment |    426 |       Floor |     16 |  Operations | (null) |      (null) |             2869 | MPL | 11-Feb-2015 12:00:00 AM |  12.39 |      (null) |     (null) |  (null) |       (null) |         (null) |      2 |
|  (null) |      (null) | 2015 |   (null) |   (null) |           (null) |      19 | 2015000999 |       Bike |          201507 |    5699 |       Parts |    426 |       Floor |     16 |  Operations |  89999 |      fridge |         MA864654 |   J | 07-Jul-2015 12:00:00 AM | 899.84 |       52254 |       Bike |  (null) |        65222 |         fridge |      4 |
|  (null) |      (null) | 2015 |   (null) |   (null) |           (null) |      19 | 2015000314 | Automotive |          201501 |    5513 | Maintenance |    426 |       Floor |     16 |  Operations |  80016 |      Mobile |         MI074755 |   J | 07-Jan-2015 12:00:00 AM |  62.44 |       52254 | Automotive |  (null) |       131100 |         Mobile |      4 |
|  (null) |         Reg | 2015 |        4 |    86.53 |          battery |      19 | 2015000314 |   Register |          201502 |    5430 |    Material |    426 |       Floor |     16 |  Operations | E80005 |     Running |           P42092 |   0 | 03-Feb-2015 12:00:00 AM | 346.12 |      (null) |     (null) |       1 |       142100 |         Runner |     10 |
|  (null) |      (null) | 2015 |   (null) |   (null) |           (null) |      19 | 2015000314 | Automotive |          201501 |    5513 | Maintenance |    426 |       Floor |     16 |  Operations |  80016 |      Mobile |         MI074755 |   J | 07-Jan-2015 12:00:00 AM |    100 |       52254 | Automotive |  (null) |       131100 |         Mobile |      4 |