SQL Server資料庫刪除資料集中重複資料執行個體講解_MsSql

來源:互聯網
上載者:User

SQL Server資料庫操作中,有時對於表中的結果集,滿足一定規則我們則認為是重複資料,而這些重複資料需要刪除。如何刪除呢?本文我們通過一個例子來加以說明。

例子如下:

如下只要companyName,invoiceNumber,customerNumber三者都相同,我們則認為是重複資料,下面的例子示範了如何刪除。

declare @InvoiceListMaster table ( ID int identity primary key ,   companyName Nchar(20),   invoiceNumber int,   CustomerNumber int,   rmaNumber int )   insert  @InvoiceListMaster   select N'華為', 1001,100,200   union all   select N'華為', 1001,100,300   union all   select N'華為', 1001,100,301   union all   select N'中興', 1002, 200,1      union all   select N'中興', 1002, 200,2   select * from @InvoiceListMaster   DELETE A   from (   select rown = ROW_NUMBER( )over( partition by companyname,   invoicenumber,   customerNumber    order by companyname,   invoicenumber,   customerNumber ),   companyname,   invoicenumber,   customerNumber   from @InvoiceListMaster )a   where exists ( select 1    from ( select rown = ROW_NUMBER( )over( partition by companyname,   invoicenumber,   customerNumber    order by companyname,   invoicenumber,   customerNumber ),   companyname,   invoicenumber,   customerNumber   from @InvoiceListMaster ) b   where b.companyName = a.companyName   and b.invoiceNumber = a.invoiceNumber   and b.CustomerNumber = a.CustomerNumber   and a.rown > b.rown   )   select * from @InvoiceListMaster 

以上的例子就示範了SQL Server資料庫刪除資料集中重複資料的過程,希望本次的介紹能夠對您有所收穫!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.