从表中删除重复记录

时间:2018-01-30 03:20:11

标签: sql oracle

我使用以下查询从数据库中选择了几条记录

    SELECT distinct po.linetot, TOT.TOTQTY, TOT.TOTamount, po.buy_qty_due, DUPS.* FROM 
          (
          select WOC.ver_type,WOC.WO_NO, WOC.COST, WOC.QTY, WOC.REQUISITION_NO, WOC.REQUISITION_LINE_NO, WOC.REQUISITION_RELEASE_NO, WOC.RELEASE_NO, WOC.VER_NO, WOC.VER_DATE, 
          WOC.INVOICE_ID, WOC.PURCHASE_ORDER_NO , WOC.PURCHASE_LINE_NO, WOC.PURCHASE_RELEASE_NO , COUNT(WOC.VER_NO) 
          from work_order_coding_tab WOC
          where WOC.ver_type='I'
          group by WOC.ver_type, WOC.WO_NO, woc.cost, WOC.QTY, WOC.REQUISITION_NO, WOC.REQUISITION_LINE_NO, WOC.REQUISITION_RELEASE_NO, WOC.RELEASE_NO, WOC.VER_NO, WOC.VER_DATE,
          WOC.INVOICE_ID, WOC.PURCHASE_ORDER_NO, WOC.PURCHASE_LINE_NO, WOC.PURCHASE_RELEASE_NO
          having  count(WOC.ver_no)>1
          ) DUPS
    LEFT JOIN     
          (
          select WOC.ver_type,WOC.WO_NO, WOC.QTY, WOC.amount, SUM (WOC.QTY) AS TOTQTY , SUM (WOC.amount) AS TOTamount, WOC.REQUISITION_NO, WOC.REQUISITION_LINE_NO, WOC.REQUISITION_RELEASE_NO, WOC.RELEASE_NO, WOC.VER_NO, WOC.VER_DATE, 
          WOC.INVOICE_ID, WOC.PURCHASE_ORDER_NO , WOC.PURCHASE_LINE_NO, WOC.PURCHASE_RELEASE_NO  
          from work_order_coding_tab WOC
          where WOC.ver_type='I'
          group by WOC.ver_type, WOC.WO_NO, woc.amount, WOC.QTY, WOC.REQUISITION_NO, WOC.REQUISITION_LINE_NO, WOC.REQUISITION_RELEASE_NO, WOC.RELEASE_NO, WOC.VER_NO, WOC.VER_DATE,
          WOC.INVOICE_ID, WOC.PURCHASE_ORDER_NO , WOC.PURCHASE_LINE_NO, WOC.PURCHASE_RELEASE_NO
          ) TOT
      ON TOT.VER_NO=DUPS.VER_NO and tot.purchase_order_no=dups.purchase_order_no and tot.purchase_line_no=dups.purchase_line_no and tot.purchase_release_no=dups.purchase_release_no

    LEFT JOIN 
          (
          select POL.order_no, POL.line_no, POL.release_no, POL.buy_qty_due, POL.buy_unit_price , POL.buy_qty_due * POL.buy_unit_price as linetot, POL.ORIGINAL_QTY from purchase_Order_line_tab  POL
          )PO
      on PO.order_no=dups.PURCHASE_ORDER_NO and PO.line_no=dups.PURCHASE_LINE_NO and po.release_no=dups.PURCHASE_RELEASE_NO


    where po.linetot<> tot.totamount
    order by dups.wo_no, dups.PURCHASE_ORDER_NO, dups.PURCHASE_LINE_NO, dups.PURCHASE_RELEASE_NO

我需要做的是从&#39; work_order_coding_tab&#39;中删除重复的记录。

但我不知道如何从表中删除重复项。

&#39; work_order_coding_tab&#39;是WO_NO和ROW没有。有没有人知道如何做到这一点

P.S我试着考虑给出的答案之一,并为前两个左连接写了以下查询

    SELECT * from WORK_ORDER_CODING_TAB WOC1
      where 
      woc1.row_no <
      ANY (
        select
        woc.row_no
                    FROM 
                    WORK_ORDER_CODING_TAB woc
          where 
            WOC.ver_type=WOC1.ver_type AND 
            WOC.WO_NO=WOC1.WO_NO AND 
            woc.cost=WOC1.cost AND
            WOC.QTY=WOC1.QTY AND 
            WOC.REQUISITION_NO=WOC1.REQUISITION_NO AND 
            WOC.REQUISITION_LINE_NO=WOC1.REQUISITION_LINE_NO AND 
            WOC.REQUISITION_RELEASE_NO=WOC1.REQUISITION_RELEASE_NO AND 
            WOC.RELEASE_NO=WOC1.RELEASE_NO AND 
            WOC.VER_NO=WOC1.VER_NO AND 
            WOC.VER_DATE=WOC1.VER_DATE AND
            WOC.INVOICE_ID=WOC1.INVOICE_ID AND 
            WOC.PURCHASE_ORDER_NO=WOC1.PURCHASE_ORDER_NO AND 
            WOC.PURCHASE_LINE_NO=WOC1.PURCHASE_LINE_NO AND 
            WOC.PURCHASE_RELEASE_NO=WOC1.PURCHASE_RELEASE_NO and 
            WOC.ver_type='I' and
            WOC.VER_NO=WOC1.VER_NO 

      )

但它并没有像第一个查询那样给我相同的结果

1 个答案:

答案 0 :(得分:0)

正如你的评论中提到的,我使用wn_no作为删除记录的关键。我正在使用row_number分析函数来删除记录。

以下查询可帮助您选择重复记录,然后您可以根据wn_no确定要从WORK_ORDER_CODING_TAB中删除哪些行。

代码:

select * from(
select taba.*,row_number() over(partition by WO_NO order by null) from (
SELECT distinct po.linetot, TOT.TOTQTY, TOT.TOTamount, po.buy_qty_due, DUPS.* FROM 
          (
          select WOC.ver_type,WOC.WO_NO, WOC.COST, WOC.QTY, WOC.REQUISITION_NO, WOC.REQUISITION_LINE_NO, WOC.REQUISITION_RELEASE_NO, WOC.RELEASE_NO, WOC.VER_NO, WOC.VER_DATE, 
          WOC.INVOICE_ID, WOC.PURCHASE_ORDER_NO , WOC.PURCHASE_LINE_NO, WOC.PURCHASE_RELEASE_NO , COUNT(WOC.VER_NO) 
          from work_order_coding_tab WOC
          where WOC.ver_type='I'
          group by WOC.ver_type, WOC.WO_NO, woc.cost, WOC.QTY, WOC.REQUISITION_NO, WOC.REQUISITION_LINE_NO, WOC.REQUISITION_RELEASE_NO, WOC.RELEASE_NO, WOC.VER_NO, WOC.VER_DATE,
          WOC.INVOICE_ID, WOC.PURCHASE_ORDER_NO, WOC.PURCHASE_LINE_NO, WOC.PURCHASE_RELEASE_NO
          having  count(WOC.ver_no)>1
          ) DUPS
    LEFT JOIN     
          (
          select WOC.ver_type,WOC.WO_NO, WOC.QTY, WOC.amount, SUM (WOC.QTY) AS TOTQTY , SUM (WOC.amount) AS TOTamount, WOC.REQUISITION_NO, WOC.REQUISITION_LINE_NO, WOC.REQUISITION_RELEASE_NO, WOC.RELEASE_NO, WOC.VER_NO, WOC.VER_DATE, 
          WOC.INVOICE_ID, WOC.PURCHASE_ORDER_NO , WOC.PURCHASE_LINE_NO, WOC.PURCHASE_RELEASE_NO  
          from work_order_coding_tab WOC
          where WOC.ver_type='I'
          group by WOC.ver_type, WOC.WO_NO, woc.amount, WOC.QTY, WOC.REQUISITION_NO, WOC.REQUISITION_LINE_NO, WOC.REQUISITION_RELEASE_NO, WOC.RELEASE_NO, WOC.VER_NO, WOC.VER_DATE,
          WOC.INVOICE_ID, WOC.PURCHASE_ORDER_NO , WOC.PURCHASE_LINE_NO, WOC.PURCHASE_RELEASE_NO
          ) TOT
      ON TOT.VER_NO=DUPS.VER_NO and tot.purchase_order_no=dups.purchase_order_no and tot.purchase_line_no=dups.purchase_line_no and tot.purchase_release_no=dups.purchase_release_no

    LEFT JOIN 
          (
          select POL.order_no, POL.line_no, POL.release_no, POL.buy_qty_due, POL.buy_unit_price , POL.buy_qty_due * POL.buy_unit_price as linetot, POL.ORIGINAL_QTY from purchase_Order_line_tab  POL
          )PO
      on PO.order_no=dups.PURCHASE_ORDER_NO and PO.line_no=dups.PURCHASE_LINE_NO and po.release_no=dups.PURCHASE_RELEASE_NO


    where po.linetot<> tot.totamount
    order by dups.wo_no, dups.PURCHASE_ORDER_NO, dups.PURCHASE_LINE_NO, dups.PURCHASE_RELEASE_NO) taba) where rn >1;