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

sql過濾重復記錄

發布時間: 2022-04-22 06:43:21

sql 如何過濾重復記錄

問題背景

在一個多表查詢的sql中正常情況下產生的數據都是唯一的,但因為資料庫中存在錯誤(某張表中存在相同的外鍵ID)導致我這邊查詢出來的數據就會有重復的問題

下面結果集中UserID:15834存在多個

參考:

MSDN: OVER 子句 (Transact-SQL)

stackoverflow sql query distinct with Row_Number

SQL Trick: row_number() is to SELECT what dense_rank() is to SELECT DISTINCT

Ⅱ sql怎樣過濾重復記錄

declare @t table (ID int, Name varchar(10), address varchar(10))
insert into @t select 1,'a','1'
insert into @t select 2,'a','2'
insert into @t select 3,'a','1'
insert into @t select 4,'a','3'
insert into @t select 5,'b','2'
insert into @t select 6,'b','2'
insert into @t select 7,'b','3'
insert into @t select 8,'b','1'
insert into @t select 9,'c','1'
insert into @t select 10,'d','2'

select ID,Name,address from
(
select *,orderid=(select count(1) from @t where name=x.name and address=x.address)
from @t as x
) as y
where orderid>=2

--結果
/*
ID Name address
----------------------------------
1 a 1
3 a 1
5 b 2
6 b 2
*/

--沒試過我的?我的測試通過啊!!!

補充:
看不懂?
很簡單的呀,就這樣:
select ID,Name,address from
(
select *,orderid=(select count(1) from 表名 where name=x.name and address=x.address)
from 表名 as x
) as y
where orderid>=2

Ⅲ sql中如何刪除一個表中重復的記錄

sql中刪除一個表中的重復記錄可以採用如下步驟:

1、把a_dist表的記錄用distinct去重,結果放到臨時表中。

select distinct * into #temp from a_dist;

2、把a_dist表的記錄全部刪除。

delete from a_dist;

3、把臨時表中的數據信息導進到a_dist表中,並刪除臨時表。

insert into a_distselect * from #temp;

drop table #temp;

(3)sql過濾重復記錄擴展閱讀:

SQL (結構化查詢語言)是用於執行查詢的語法。在資料庫上執行的大部分工作都由 SQL 語句完成。SQL 語言包含用於更新、插入和刪除記錄的語法。

增刪改查指令構成了 SQL 的 DML 部分:

  • SELECT- 從資料庫表中獲取數據

  • UPDATE- 更新資料庫表中的數據

  • DELETE- 從資料庫表中刪除數據

  • INSERT INTO- 向資料庫表中插入數據

Ⅳ 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

(4)sql過濾重復記錄擴展閱讀

1、查找表中多餘的重復記錄,重復記錄是根據單個欄位(peopleId)來判斷

select * from people

where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

2、刪除表中多餘的重復記錄,重復記錄是根據單個欄位(peopleId)來判斷,只留有rowid最小的記錄

delete from people

where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)

and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)

3、查找表中多餘的重復記錄(多個欄位)

select * from vitae a

where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)

Ⅳ sql語句查詢過濾重復數據

1、查找表中多餘的重復記錄,重復記錄是根據單個欄位(peopleId)來判斷

復制代碼代碼如下:

select * from people
where peopleId in (select peopleId from people group by peopleId having count

(peopleId) > 1)

2、刪除表中多餘的重復記錄,重復記錄是根據單個欄位(peopleId)來判斷,只留有rowid最小的記錄

復制代碼代碼如下:

delete from people
where peopleId in (select peopleId from people group by peopleId having count

(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId
)>1)

Ⅵ SQL查詢語句,怎樣查詢重復數據

1、第一步,打開資料庫,並創建一個包含重復數據的新用戶表,見下圖,轉到下面的步驟。

Ⅶ 用SQL語句怎麼過濾重復數據

有一半是添加表的,因為我沒有你的結果集,所以拼了個表變數做為結果集
,重點在後半部分,處理邏輯是按你的想寫的,前提是如果我沒有理解錯的話
這個方法的結果集返回的是每一年的數據,年數遞增的,行數以有多少個城市為准,不過我感覺你要這樣的結果集沒有什麼意義

declare @tab table(name nvarchar(20), both int)
declare @tabtmp table(name nvarchar(20), both int)
declare @tabname table(name nvarchar(20))
declare @name nvarchar(20)
declare @both int

insert into @tab
select N'上海',1996
union
select N'上海',1997
union
select N'北京',1996
union
select N'北京', 1997

insert into @tabname
select distinct name from @tab

select top 1 @name=name from @tab order by name asc
select @both=MIN(both) from @tab

while(@name is not null)
begin
insert into @tabtmp
select @name,@both

update @tab set name='' where name=@name
set @name=null
select top 1 @name =name from @tab where name<>'' order by name asc
select top 1 @both=both from @tab where both>@both order by both asc
end

select * from @tabtmp

Ⅷ sql查詢去掉重復記錄

1、打開要去掉重復數據的資料庫,這里新建一張含有重復數據的user表做示例,如下圖所示:

Ⅸ sql 查詢語句 資料庫 過濾重復記錄

使用分析函數row_number(大部分資料庫的新頒布都支持),對數據按你需要的重復欄位進行編號,然後只取編號值為1的記錄。
類似於:
select d.*
from (
-- 按mobile, area, address, post_code對記錄進行分組排序,並且按accept_name升序排
select row_number() over (group by mobile, area, address, post_code order by accept_name) as row_idx, s.*
from dt_orders s
) d
where d.row_idx = 1

Ⅹ 怎麼用SQL篩選資料庫重復記錄

用group by語句可以篩選重復數據。

1、創建測試表、插入數據

createtabletest
(idint,
namevarchar(10));

insertintotestvalues(1,'張三')
insertintotestvalues(2,'李四')
insertintotestvalues(3,'王五')
insertintotestvalues(4,'趙六')
insertintotestvalues(1,'張三')
insertintotestvalues(2,'李四')

2、現在要篩選出重復數據,使查詢的數據不重復,可用語句

select id,name from test group by id,name;

3、結果如圖: