常用sql:过滤重复数据查询如何写

hxy102863729 2010-06-03 04:56:18
RT
有表[aa]
id name
1 张三
2 李四
2 李四
2 ‘’
3 王五
3 王五

过滤id重复的SQL?
...全文
637 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxy102863729 2010-06-03
  • 打赏
  • 举报
回复
To htl258
您的这个可以
select DISTINCT * from tb t where name =(select top 1 name from tb where id=t.id)
结贴
hxy102863729 2010-06-03
  • 打赏
  • 举报
回复
name 无规律 就是null 和有数据2种情况
htl258_Tony 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 hxy102863729 的回复:]
引用 14 楼 xys_777 的回复:

SQL code


select id1=identity (int,1,1),* into #t from aa where name is not null and ltrim(name) <>''

select id,name from #t t1 where not exists(select 1 from #t where n……
[/Quote]
name这一列,没什么规律吗,什么情况都有吗?
hxy102863729 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 xys_777 的回复:]

SQL code


select id1=identity (int,1,1),* into #t from aa where name is not null and ltrim(name) <>''

select id,name from #t t1 where not exists(select 1 from #t where name=t1.name and id1<t1.id1……
[/Quote]
貌似我这得到的数据不对,而且能不用临时表么,
我只想把id重复的过滤。其他字段不为null的随便取一个就行
永生天地 2010-06-03
  • 打赏
  • 举报
回复


select id1=identity (int,1,1),* into #t from aa where name is not null and ltrim(name) <>''

select id,name from #t t1 where not exists(select 1 from #t where name=t1.name and id1<t1.id1)
hxy102863729 2010-06-03
  • 打赏
  • 举报
回复
求解!!!
hxy102863729 2010-06-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 htl258 的回复:]

SQL code
--> 生成测试数据表: [tb]
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([id] [int],[name] [nvarchar](10))
INSERT INTO [tb]
SELECT '1','张三' UNION ALL
SELECT '2','李四' UNI……
[/Quote]


--> 生成测试数据表: [tb]
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([id] [int],[name] [nvarchar](10))
INSERT INTO [tb]
SELECT '1','张三' UNION ALL
SELECT '2','李四' UNION ALL
SELECT '2','..' UNION ALL

SELECT '3','王五' UNION ALL
SELECT '3','王五'

改为这样 查询就有问题 我想得到原来结果 或者

id name
----------- ----------
1 张三
2 ..
3 王五


我想id唯一。
shixixi1987 2010-06-03
  • 打赏
  • 举报
回复
select id from table1 where [name] is not null group by id having count(1)>1
go
hxy102863729 2010-06-03
  • 打赏
  • 举报
回复
我的意思要id 不允许重复,
htl258_Tony 2010-06-03
  • 打赏
  • 举报
回复
--> 生成测试数据表: [tb]
IF OBJECT_ID('[tb]') IS NOT NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb] ([id] [int],[name] [nvarchar](10))
INSERT INTO [tb]
SELECT '1','张三' UNION ALL
SELECT '2','李四' UNION ALL
SELECT '2','李四' UNION ALL
SELECT '2','' UNION ALL
SELECT '3','王五' UNION ALL
SELECT '3','王五'


-->SQL查询如下:
SELECT DISTINCT * FROM [tb] WHERE ISNULL(name,'')<>''
/*
id name
----------- ----------
1 张三
2 李四
3 王五

(3 行受影响)
*/
hxy102863729 2010-06-03
  • 打赏
  • 举报
回复
SQL2000
hxy102863729 2010-06-03
  • 打赏
  • 举报
回复
name 取不为NULL中选取一个
东那个升 2010-06-03
  • 打赏
  • 举报
回复
select * from aa a
where (select count(1) from aa where a.id=id)=1
水族杰纶 2010-06-03
  • 打赏
  • 举报
回复
;with ldslove
as(
select *,
px=row_number()over(partition by id order by (select 1))
from [aa])
select *
from aa
where px=1
htl258_Tony 2010-06-03
  • 打赏
  • 举报
回复
select distindt * from aa t where name=(select top 1 name from aa where id=t.id)
bill024 2010-06-03
  • 打赏
  • 举报
回复
select distinct id,name from [aa]
东那个升 2010-06-03
  • 打赏
  • 举报
回复
select id from tb 
group by id
having count(1)>1
东那个升 2010-06-03
  • 打赏
  • 举报
回复
name 怎么筛选

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧