SQL查询中如何剔除重复

来源:百度知道 编辑:UC知道 时间:2024/06/04 10:58:56
例如:select username, date from user_info
where date>=to_date('2008-8-1 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
and date<=to_date('2008-8-25 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
group by username, date
查询结果中同一username的date有多个,我只想保留一个,应该怎么修改语句?谢谢!

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

excel中剔除重复数据 如何在SQL SERVER中编写查询重复记录的存储过程,请教! 在excel如何剔除重复数据? sql 语言中如何查询 sql 查询表中所有记录但 不重复 SQL查询去掉重复值 sql 查询删除重复行? sql查询消除重复记录 SQL查询重复的问题 sql查询去掉重复记录