sql过滤重复行
1. 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
2. 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
3. sql server 过滤重复记录的语法
select * from chengji a where not exists(select 1 from chengji where Name=a.Name and age=a.age and ID<a.ID)--取小ID值
--or
select * from chengji a where not exists(select 1 from chengji where Name=a.Name and age=a.age and ID>a.ID)--取大ID值
--or
select * from chengji a where ID=(select min(ID) from chengji where Name=a.Name and age=a.age )--取小ID值,可用max(ID)
delete时
delete a from chengji a where exists(select 1 from chengji where Name=a.Name and age=a.age and ID>a.ID)
or
delete a from chengji a where exists(select 1 from chengji where Name=a.Name and age=a.age and ID<a.ID)
delete a from chengji a where ID not in(select min(ID) from chengji where Name=a.Name and age=a.age)
4. 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 [去除重复的字段名列表,....])
(4)sql过滤重复行扩展阅读:
SQL Server 是Microsoft 公司推出的关系型数据库管理系统。具有使用方便可伸缩性好与相关软件集成程度高等优点,可跨越从运行Microsoft Windows 98 的膝上型电脑到运行Microsoft Windows 2012 的大型多处理器的服务器等多种平台使用。
Microsoft SQL Server 是一个全面的数据库平台,使用集成的商业智能 (BI)工具提供了企业级的数据管理。Microsoft SQL Server数据库引擎为关系型数据和结构化数据提供了更安全可靠的存储功能,使您可以构建和管理用于业务的高可用和高性能的数据应用程序。
5. sql 查询语句中如何根据条件去除部分重复的行
给你个思路,按ID 分组,date降序给数据添加行号,条件是你传的日期,再加上条件行号=1就是你要的,你先自己写写看看能不能,肯定是可以实现的!
6. sql 查询去除重复行
在select 后面加一个distinct应该就可以了,不过不建议这么做,建议找出为什么重复的原因。能否把group by所有的字段的select 结果都给我们看看
select distinct
SFM_BH as 住院号,SFM_XM as 姓名,SFM_JZBZ 标志, SFM_JF 缴费合计,SFM_DF 费用合计,SFM_YE 余额 from
(select SFD_BH,SFD_CZY,SFD_RQ,SFD_ZFLB,SFD_ZY from ZY_SFD
where
SFD_JDLB=0
and SFD_BZ=1
and DATEDIFF(Day, SFD_RQ, '2015-09-26 15:18:00') <= 0
and DATEDIFF(Day, SFD_RQ, '2015-10-25 15:18:00') >= 0 )
as acc left
join
ZY_SFM on (SFM_BH=SFD_BH)
Where 1=1
group by
SFM_BH,SFM_XM,SFM_RYRQ,SFM_CW,SFD_RQ,SFM_JZBZ,SFM_JF,SFM_DF,SFM_YE,SFD_ZFLB
Order by
SFM_BH,convert(char(16),SFM_RYRQ,120)DESC,SFD_RQ
7. sql去除重复行
这个简单了,相信你要的只是查询结果
但是重复的只是名字吧?
select disinct name from a这样就可以了
如果想显示其他列,可以告诉你这是不可能的
比如说 小红 那后面对应的日期和前面的ID你要计算机来显示哪个?
8. 如何去除个别字段重复的行SQL如何写
select*fromtable_namewhereidin
(
selectmin(id)fromtable_namegroupby体重
)
试下以上代码
9. 用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
10. SQL重复数据的筛选
你要看你有哪些数来据段是相同的源,就根据那些相同的数据段分类。
比如说,
A B C D
1 1 1 3
1 1 1 4
1 1 1 5
(前面的insert 我就不写了)
那就是select A,B,C,MAX(D) FROM TABLE GROUP BY A,B,C
如果是
A B C D
1 1 1 2
2 1 1 3
3 1 1 4
就是说,如果你还有一个字段是id,主键的话就是
select A,B,C,MAX(D) FROM TABLE GROUP BY B,C