sql除冗餘

有重複數據主要有一下幾種情況:
1.存在兩條完全相同的紀錄
這是最簡單的一種情況,用關鍵字distinct就可以去掉
example: select distinct * from table(表名) where (條件)

2.存在部分字段相同的紀錄(有主鍵id即唯一鍵)
如果是這種情況的話用distinct是過濾不了的,這就要用到主鍵id的唯一性特點及group by分組
example:
select * from table where id in (select max(id) from table group by [去除重複的字段名列表,…])

3.沒有唯一鍵ID
這種情況我覺得最複雜,目前我只會一種方法,有那位知道其他方法的可以留言,交流一下:
example:
select identity(int1,1) as id,* into newtable(臨時表) from table
select * from newtable where id in (select max(id) from newtable group by [去除重複的字段名列表,…])
drop table newtable

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章