求一条SQL,去掉重复数据

wxylvmnn 2012-05-10 09:50:28
表T的数据如下,
A B
-------
A1 B1
A1 B2
A2 B1
A3 B2

希望得到
A B
--------
A1 B1
A2 B2
A3
...全文
160 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
anzhiqiang_touzi 2012-05-10
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 的回复:]
SQL code

create table T (a varchar(16), b varchar(16))
insert into T
select 'A1','B1' union
select 'A1','B2' union
select 'A2','B1' union
select 'A3','B2'

declare @tb_a table (id int identi……
[/Quote]
tim_spac_126 2012-05-10
  • 打赏
  • 举报
回复
create table T (a varchar(16), b varchar(16))
insert into T
select 'A1','B1' union
select 'A1','B2' union
select 'A2','B1' union
select 'A3','B2'

declare @tb_a table (id int identity(1,1), ch varchar(16))
declare @tb_b table (id int identity(1,1), ch varchar(16))

insert into @tb_a
select distinct a
from T
order by a

insert into @tb_b
select distinct b
from T
order by b

select i.id, a.ch, b.ch
from (select id from @tb_a union select id from @tb_b) as i
left join @tb_a as a on a.id = i.id
left join @tb_b as b on b.id = i.id

drop table T


-- id ch ch
-- 1 A1 B1
-- 2 A2 B2
-- 3 A3
tim_spac_126 2012-05-10
  • 打赏
  • 举报
回复
create table T (a varchar(16), b varchar(16))
insert into T
select 'A1','B1' union
select 'A1','B2' union
select 'A2','B1' union
select 'A3','B2'

declare @tb_a table (id int identity(1,1), ch varchar(16))
declare @tb_b table (id int identity(1,1), ch varchar(16))

insert into @tb_a
select distinct a
from T
order by a

insert into @tb_b
select distinct b
from T
order by b

select i.id, a.ch, b.ch
from (select id from @tb_a union select id from @tb_b) as i
left join @tb_a as a on a.id = i.id
left join @tb_b as b on b.id = i.id

drop table T


-- id ch ch
-- 1 A1 B1
-- 2 A2 B2
-- 3 A3
tim_spac_126 2012-05-10
  • 打赏
  • 举报
回复
create table T (a varchar(16), b varchar(16))
insert into T
select 'A1','B1' union
select 'A1','B2' union
select 'A2','B1' union
select 'A3','B2'

declare @tb_a table (id int identity(1,1), ch varchar(16))
declare @tb_b table (id int identity(1,1), ch varchar(16))

insert into @tb_a
select distinct a
from T
order by a

insert into @tb_b
select distinct b
from T
order by b

select i.id, a.ch, b.ch
from (select id from @tb_a union select id from @tb_b) as i
left join @tb_a as a on a.id = i.id
left join @tb_b as b on b.id = i.id

drop table T


-- id ch ch
-- 1 A1 B1
-- 2 A2 B2
-- 3 A3
libystu 2012-05-10
  • 打赏
  • 举报
回复
利用游标好了
libystu 2012-05-10
  • 打赏
  • 举报
回复
有点难度啊
--小F-- 2012-05-10
  • 打赏
  • 举报
回复
;with f as
(
select id=row_number()over(partition by a order by getdate()),* from tb
)
select
isnull(a.a,'') as a,isnull(b.b,'') as b
from
(select px=row_number()over(order by getdate()),* from (select distinct id,a from f where id=1)t)a
left join
(select px=row_number()over(order by getdate()),* from (select distinct id,b from f where id=1)t)b
on
a.px=b.px
夏小笨 2012-05-10
  • 打赏
  • 举报
回复
select distinct a,b from tb
  • 打赏
  • 举报
回复

select min(A) as A,isnull(min(B),'') AS B from tbl
zhousq00 2012-05-10
  • 打赏
  • 举报
回复
你这个没有一个去除规则啊
十三门徒 2012-05-10
  • 打赏
  • 举报
回复
你给的数据不对吧 你看看你给的数据是怎么个重复法啊
wxylvmnn 2012-05-10
  • 打赏
  • 举报
回复
都不对啊。。。
结果变成了
A1 B1
A2 B2
A3 B1
想要的是
A1 B1
A2 B2
A3
Me_zzx 2012-05-10
  • 打赏
  • 举报
回复

select a,min(b) b from tb group by a
--小F-- 2012-05-10
  • 打赏
  • 举报
回复
select * from tb t where not exists(select 1 from tb where a=t.a and b<t.b)
长垣小康 2012-05-10
  • 打赏
  • 举报
回复
select distict(*) from T

34,590

社区成员

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

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