久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

僅在sql server中的條件下刪除重復記錄

Delete duplicate records only on condition in sql server(僅在sql server中的條件下刪除重復記錄)
本文介紹了僅在sql server中的條件下刪除重復記錄的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想刪除重復項,例如我想刪除這一行Test2, 321, 0",因為有重復項,但在第 1 行和第 2 行中,我只想刪除第 2 行,因為類型 id 高于當前在第一行重復.

這是我的桌子

<前>ID、名稱、記錄編號、類型--------------------------1, 測試, 123, 02, 測試, 123, 13, 測試 2, 321, 04、測試2、321、0

我可以使用此查詢刪除第 4 行中的重復項.但我似乎無法弄清楚如何刪除第 2 行,因為第 1 行相同但類型編號較低.如果類型編號為 2,則獲勝,您必須刪除具有相同名稱和記錄編號的 0 和 1 類型編號的任何重復項.

WITH dup2 as (選擇名稱, 記錄號, 鍵入 ROW_NUMBER() OVER(PARTITION BY Name, RecordNum, Type ORDER BY ID ASC) AS NumOfDups從 MyTbale)從 dup2 中刪除 NumOfDups >1

解決方案

所以基本上你只想為每個 Name, RecordNum 組保留一條記錄.如果Type相同只保留最低的ID,如果Type不同保留最低的類型:

帶有 dup2 AS(選擇姓名,記錄數,NumOfDups = ROW_NUMBER()OVER(按名稱分區,RecordNumORDER BY CASE WHEN Type=2 THEN 0 ELSE 1 END, Type, Id)從 MyTbale)刪除從 dup2WHERE NumOfDups >1

SQL-Fiddle 演示

編輯根據評論:假設當 type = 2 時,我想刪除類型為 0 或 1 的其他匹配記錄,因此這里有兩個獲勝."

I want to delete duplicates for example I want to delete this row "Test2, 321, 0" because there is a duplicate, but in row 1 and 2 I only want to delete row two because the type id is higher then the current duplicate in row one.

This is my table

ID, Name, RecordNum, Type
--------------------------
1, Test, 123, 0
2, Test, 123, 1
3, Test2, 321, 0
4, Test2, 321, 0

I can delete duplicate in row 4 using this query. but I cannot seem to figure out how to delete row 2 because row 1 is the same but type number is lower. and if the type number is 2 it wins and you have to delete any duplicates with 0 and 1 type numbers that have the same name and recordnum.

WITH dup2 as ( 
  SELECT Name
       , RecordNum
       , Type ROW_NUMBER() OVER(PARTITION BY Name, RecordNum, Type ORDER BY ID ASC) AS NumOfDups 
    FROM MyTbale) 
  delete FROM dup2 WHERE NumOfDups > 1

解決方案

So basically you want to keep only one record for each Name, RecordNum group. If the Type is the same keep only the lowest ID, if the Type is different keep the lowest type:

WITH dup2 AS 
( 
         SELECT  NAME, 
                 RecordNum, 
                 NumOfDups = ROW_NUMBER()OVER(
                                 PARTITION BY NAME, RecordNum 
                                 ORDER BY CASE WHEN Type=2 THEN 0 ELSE 1 END, Type, Id)
         FROM    MyTbale) 
DELETE 
FROM   dup2 
WHERE  NumOfDups > 1

SQL-Fiddle Demo

Edited according to comment: "Let say when the type = 2 then I want to delete the other matching records that have a zero or one for type, so two wins here."

這篇關于僅在sql server中的條件下刪除重復記錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 免费黄色大片 | 欧美一级在线 | 国产中文字幕网 | 国产亚洲一区二区精品 | 亚洲人成人一区二区在线观看 | 激情小视频 | www.天天操 | 日本免费一区二区三区视频 | 国产视频一区二区 | 亚洲一区二区三区在线视频 | 国产国语精品 | 成人av网页 | av在线一区二区三区 | 国产精品亚洲综合 | 日韩精品久久一区 | 久久久久国产精品免费免费搜索 | 日韩久久综合网 | 2018中文字幕第一页 | 视频1区| 久久中文网 | 色欧美片视频在线观看 | 日韩图区| 精品国产一区二区三区在线观看 | 亚洲在线免费观看 | 国产在线观看网站 | 亚洲精品一区二区在线观看 | 一区二区免费在线 | 日韩一区二区福利 | 91p在线观看 | 国产精品久久久久久亚洲调教 | 91视频官网 | 日韩一区二区三区四区五区六区 | 在线观看成人小视频 | 成人网视频 | 99国产精品久久久久老师 | 久久激情视频 | 国产在线精品一区二区三区 | 亚洲人免费视频 | 国产精品美女www爽爽爽 | 亚洲免费在线播放 | 亚洲一区高清 |