Ⅰ 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、结果如图: