国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > SQL Server > 正文

SQL Server數(shù)據(jù)庫(kù)刪除數(shù)據(jù)集中重復(fù)數(shù)據(jù)實(shí)例講解

2024-08-31 01:03:10
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文通過(guò)一個(gè)例子介紹了SQL Server數(shù)據(jù)庫(kù)中刪除數(shù)據(jù)集中的重復(fù)數(shù)據(jù)的操作過(guò)程,需要的朋友可以參考下

SQL Server數(shù)據(jù)庫(kù)操作中,有時(shí)對(duì)于表中的結(jié)果集,滿足一定規(guī)則我們則認(rèn)為是重復(fù)數(shù)據(jù),而這些重復(fù)數(shù)據(jù)需要?jiǎng)h除。如何刪除呢?本文我們通過(guò)一個(gè)例子來(lái)加以說(shuō)明。

例子如下:

如下只要companyName,invoiceNumber,customerNumber三者都相同,我們則認(rèn)為是重復(fù)數(shù)據(jù),下面的例子演示了如何刪除。

 

  1. declare @InvoiceListMaster table ( ID int identity primary key ,  
  2.  
  3. companyName Nchar(20),  
  4.  
  5. invoiceNumber int,  
  6.  
  7. CustomerNumber int,  
  8.  
  9. rmaNumber int )  
  10.  
  11. insert @InvoiceListMaster  
  12.  
  13. select N'華為', 1001,100,200  
  14.  
  15. union all 
  16.  
  17. select N'華為', 1001,100,300  
  18.  
  19. union all 
  20.  
  21. select N'華為', 1001,100,301  
  22.  
  23. union all 
  24.  
  25. select N'中興', 1002, 200,1  
  26.  
  27. union all 
  28.  
  29. select N'中興', 1002, 200,2  
  30.  
  31. select * from @InvoiceListMaster  
  32.  
  33. DELETE A  
  34.  
  35. from (  
  36.  
  37. select rown = ROW_NUMBER( )over( partition by companyname,  
  38.  
  39. invoicenumber,  
  40.  
  41. customerNumber  
  42.  
  43. order by companyname,  
  44.  
  45. invoicenumber,  
  46.  
  47. customerNumber ),  
  48.  
  49. companyname,  
  50.  
  51. invoicenumber,  
  52.  
  53. customerNumber  
  54.  
  55. from @InvoiceListMaster )a  
  56.  
  57. where exists ( select 1  
  58.  
  59. from ( select rown = ROW_NUMBER( )over( partition by companyname,  
  60.  
  61. invoicenumber,  
  62.  
  63. customerNumber  
  64.  
  65. order by companyname,  
  66.  
  67. invoicenumber,  
  68.  
  69. customerNumber ),  
  70.  
  71. companyname,  
  72.  
  73. invoicenumber,  
  74.  
  75. customerNumber  
  76.  
  77. from @InvoiceListMaster ) b  
  78.  
  79. where b.companyName = a.companyName  
  80.  
  81. and b.invoiceNumber = a.invoiceNumber  
  82.  
  83. and b.CustomerNumber = a.CustomerNumber  
  84.  
  85. and a.rown > b.rown  
  86.  
  87. )  
  88.  
  89. select * from @InvoiceListMaster  

以上的例子就演示了SQL Server數(shù)據(jù)庫(kù)刪除數(shù)據(jù)集中重復(fù)數(shù)據(jù)的過(guò)程,希望本次的介紹能夠?qū)δ兴斋@!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 岳阳市| 读书| 马边| 汝州市| 岳普湖县| 且末县| 鄂托克前旗| 金山区| 当涂县| 尼勒克县| 安康市| 龙南县| 司法| 舒城县| 周宁县| 南乐县| 慈溪市| 抚州市| 彩票| 尼勒克县| 晋中市| 拉孜县| 庆云县| 塘沽区| 滁州市| 宁城县| 沾益县| 建湖县| 凯里市| 冀州市| 高邮市| 偃师市| 望奎县| 西华县| 松阳县| 大化| 曲阜市| 鸡东县| 二连浩特市| 裕民县| 巴林右旗|