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

僅在sql server中的條件下刪除重復(fù)記錄

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

問題描述

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

這是我的桌子

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

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

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(選擇姓名,記錄數(shù),NumOfDups = ROW_NUMBER()OVER(按名稱分區(qū),RecordNumORDER BY CASE WHEN Type=2 THEN 0 ELSE 1 END, Type, Id)從 MyTbale)刪除從 dup2WHERE NumOfDups >1

SQL-Fiddle 演示

編輯根據(jù)評論:假設(shè)當(dāng) 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."

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

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

相關(guān)文檔推薦

Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關(guān)名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運(yùn)算符字符串中刪除重復(fù)項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關(guān)系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 欧美精品三区 | www.久久 | 精品视频一区二区三区在线观看 | 久久精品一区二区三区四区 | 亚洲影音先锋 | 国产美女在线播放 | 精品国产一区二区三区免费 | 国产精品一区视频 | 亚洲精品一区二区在线 | 97超碰免费 | 国产美女永久免费无遮挡 | 久久五月婷| 成人免费视频网站在线看 | 午夜免费| 久久久91精品国产一区二区三区 | 天堂资源视频 | 日韩在线欧美 | 亚洲午夜精品视频 | 91成人在线视频 | 五十女人一级毛片 | 亚洲国产欧美精品 | 国产91丝袜 | 成人午夜免费福利视频 | 国产精品久久久久久一区二区三区 | 欧美在线亚洲 | 久久精品国产一区二区电影 | 中文字幕在线观看第一页 | 中文字幕在线一区二区三区 | 免费看一区二区三区 | 日本三级电影在线看 | 欧美黄色片在线观看 | 亚洲国产一区二区三区在线观看 | 91免费在线看 | 亚洲黄色av网站 | 国产一区二区三区 | 国产一区二区三区精品久久久 | 91色站| 欧美电影在线 | 亚洲精品久久久一区二区三区 | 九九热在线观看视频 | 7777久久|