消除重複行

快乐_石头 2008-11-13 01:47:56
ID,Name
1,'A'
1,'A'
1,'A'
2,'B'

顯示成
1 A
A
A
2 B
...全文
94 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
linguojin11 2008-11-15
  • 打赏
  • 举报
回复
if object_id('test') is not null
drop table test
if object_id('tempdb..#t') is not null
drop table #t
if object_id('tempdb..#t1') is not null
drop table #t1
go
create table test(id int,name varchar(10))
insert into test
select 1,'A'
union all select 1,'A'
union all select 1,'A'
union all select 2,'B'
select id1=identity(int,1,1),id,name into #t from test
select * from #t
update #t
set id=null
from #t m
where exists(select 1 from #t where m.name=#t.name and m.id1>#t.id1)
select id,name into #t1 from #t
select *
from #t1

(所影响的行数为 4 行)


(所影响的行数为 4 行)

id1 id name
----------- ----------- ----------
1 1 A
2 1 A
3 1 A
4 2 B

(所影响的行数为 4 行)


(所影响的行数为 2 行)


(所影响的行数为 4 行)

id name
----------- ----------
1 A
NULL A
NULL A
2 B

(所影响的行数为 4 行)
claro 2008-11-13
  • 打赏
  • 举报
回复
[Quote=]

1 A
2 B
[/Quote]
是要显示成这样?
claro 2008-11-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 Happy_Stone 的帖子:]
ID,Name
1,'A'
1,'A'
1,'A'
2,'B'

顯示成
1 A
A
A
2 B
[/Quote]
这有什么区别?

34,589

社区成员

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

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