Sql去重一些技巧
下午的時(shí)候遇到點(diǎn)問(wèn)題,Sql去重,簡(jiǎn)單的去重可以用?DISTINCT 關(guān)鍵字去重,不過(guò),很多情況下用這個(gè)解決不了問(wèn)題。重復(fù)的數(shù)據(jù)千變?nèi)f化,例如:類似于qq、微信的最近聯(lián)系人功能,讀取這些數(shù)據(jù)肯定要和消息表關(guān)聯(lián),那樣關(guān)聯(lián)的數(shù)據(jù)會(huì)出現(xiàn)很多重復(fù)的,只是消息和時(shí)間不一樣。最終的決定條件是最后一次發(fā)消息的時(shí)間。那么問(wèn)題來(lái)了,如何做呢
SELECT colName FROM ( SELECT MAX(cloName1),cloName2..... FROM tableName(關(guān)聯(lián)表) GROUP BY colName ) t ORDER BY t.colName?這句sql語(yǔ)句只能過(guò)濾數(shù)字類型和時(shí)間,對(duì)于其他的去重條件還是不夠的,可以用ROW_NUMBER()來(lái)做,Sql如下:
SELECT t.Id,t.PCName,t.rowid FROM (SELECT Id,PCName,ROW_NUMBER() OVER (PARTITION BY PCName ORDER BY Id) AS rowidFROM dbo.PC ) AS t WHERE t.rowid = 1;下面是根據(jù)having去做的例子:
? ?a.根據(jù)某一列查詢重復(fù)數(shù)據(jù)Sql如下:
select * from A where Id in (select Id from A group by Id having count(Id) > 1)根據(jù)多個(gè)列查詢重復(fù)數(shù)據(jù)Sql如下:
select * from A a where (a.Id,a.gender) in (select Id,gender from A group by Id,gender having count(*) > 1)?
轉(zhuǎn)載于:https://www.cnblogs.com/bobo-pcb/p/4702751.html
總結(jié)
- 上一篇: 使用 Jest 和 Enzyme 测试
- 下一篇: windows10风格 springbo