當前位置:首頁 » 編程語言 » sql2008刪除重復行
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql2008刪除重復行

發布時間: 2022-04-18 23:51:19

1. sql如何刪除重復數據

select
欄位1,欄位2,欄位3
from
table
group
by
欄位1,欄位2,欄位3
having
count(*)>1
用上邊這句能找出所有重復的數據
欄位1,2,3你替換成你表裡的欄位名,如果有更多欄位的話,你就繼續添加,最後group
by的時候不要忘記了
刪除的時候要建立一個臨時表
create
table
new_table
as
select
欄位1,欄位2,欄位3
from
old_table
group
by
欄位1,欄位2,欄位3;
然後刪除原表數據
truncate
table
old_table;
然後把臨時表數據反插回去
insert
into
new_table
select
*
from
old_table;

2. SQL Server中怎樣可以從SELECT語句的結果集中刪除重復行

在要刪除的有重復數據中存在幾種情況:

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 [去除重復的欄位名列表,....])

(2)sql2008刪除重復行擴展閱讀:

SQL Server 是Microsoft 公司推出的關系型資料庫管理系統。具有使用方便可伸縮性好與相關軟體集成程度高等優點,可跨越從運行Microsoft Windows 98 的膝上型電腦到運行Microsoft Windows 2012 的大型多處理器的伺服器等多種平台使用。

Microsoft SQL Server 是一個全面的資料庫平台,使用集成的商業智能 (BI)工具提供了企業級的數據管理。Microsoft SQL Server資料庫引擎為關系型數據和結構化數據提供了更安全可靠的存儲功能,使您可以構建和管理用於業務的高可用和高性能的數據應用程序。

3. SQL消除結果集中的重復行

distinct
關鍵字可從
select
語句的結果中除去重復的行。如果沒有指定
distinct,那麼將返回所有行,包括重復的行。例如,如果在
titleauthor
中選擇所有作者
id
時未使用
distinct,那麼將會返回下列行(其中包括一些重復的行):
use
pubs

4. 如何刪除 SQL Server 表中的重復行

一個最簡單的方法,distinct去重復知道吧~用語句把所有去掉重復的記錄查出來放進表A中,然後把表A的名字改成原來的,原來的刪掉

5. SQL如何刪除重復的數據行

SQL Server刪除重復行是我們最常見的操作之一,下面就為您介紹六種適合不同情況的SQL Server刪除重復行的方法,供您參考。
1.如果有ID欄位,就是具有唯一性的欄位
delect table tableName where id not in ( select max(id) from table group by col1,col2,col3... )
group by 子句後跟的欄位就是你用來判斷重復的條件,如只有col1,那麼只要col1欄位內容相同即表示記錄相同。
2. 如果是判斷所有欄位也可以這樣 ,【對於表中的指定的欄位的進行檢查是否相同】
select * into #temp from tablename group by id1,id2,....
delete tablename
insert into table select * from #temp
drop table #temp
3. 首先去重復,再獲取N*1條數據插入到臨時表中,【對於表中的所有欄位的進行檢查是否相同】,再將原表的數據刪除,然後將臨時表的數據插入到原表,最後刪除臨時表。
select distinct * into #temp from tablename
delete tablename
go
insert tablename select * from #temp
go
drop table #temp

4. 沒有ID的情況
select identity(int,1,1) as id,* into #temp from tabel
delect # where id not in (
select max(id) from # group by col1,col2,col3...)
delect table
inset into table(...)
select ..... from #temp
5. col1+','+col2+','...col5 聯合主鍵
select * from table where col1+','+col2+','...col5 in (
select max(col1+','+col2+','...col5) from table
where having count(*)>1
group by col1,col2,col3,col4
)
group by 子句後跟的欄位就是你用來判斷重復的條件,如只有col1,那麼只要col1欄位內容相同即表示記錄相同。
6.
select identity(int,1,1) as id,* into #temp from tabel
select * from #temp where id in (
select max(id) from #emp where having count(*)>1 group by col1,col2,col3...)

6. sql server 2008如何將表中多餘重復數據刪除

你自己找到方法了,還問?
不過你這樣寫,效率會比較低,不能用索引。
你應該用exsist去寫。

你試試樣吧:
delete from OrderBaby ta where
exists
(
select count(*) as c from OrderBaby tb where ta.ObbbabyNo = tb.ObbbabyNo and ta.ObbOrderNo= tb.ObbOrderNo
having count(*) > 1
)
and ObbId not in (select min(ObbId) from OrderBaby group by ObbbabyNo+ObbOrderNo having count(*)>1)

7. SQL 語句去掉重復問題!

SQL>delete
cz
where
(id,forecid)
in
(select
id,forecid
from
table
group
by
id,forecid
having
count(*)>1)
and
rowid
not
in
(select
min(rowid)
from
table
group
by
id,forecid
having
count(*)>1);
SQL>delete
table
where
rowid
not
in(select
min(rowid)
from
cz
group
by
id,forecid
這兩個方法都可以,適用於oracle刪除大量重復數據!