首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

求一条SQL,去掉重复数据解决思路

2012-05-12 
求一条SQL,去掉重复数据表T的数据如下,AB-------A1B1A1B2A2B1A3B2希望得到AB--------A1B1A2B2A3[解决办法]

求一条SQL,去掉重复数据
表T的数据如下,
A B
-------
A1 B1
A1 B2
A2 B1
A3 B2

希望得到
A B
--------
A1 B1
A2 B2
A3


[解决办法]

SQL code
select * from tb t where not exists(select 1 from tb where a=t.a and b<t.b)
[解决办法]
SQL code
create table T (a varchar(16), b varchar(16))insert into Tselect 'A1','B1' unionselect 'A1','B2' unionselect 'A2','B1' unionselect '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_aselect distinct a from T order by ainsert into @tb_bselect distinct bfrom T order by bselect i.id, a.ch, b.chfrom (select id from @tb_a union select id from @tb_b) as ileft join @tb_a as a on a.id = i.idleft join @tb_b as b on b.id = i.iddrop table T-- id    ch    ch-- 1    A1    B1-- 2    A2    B2-- 3    A3 

热点排行