表内有重复记录的,只返回一条,求SQL语句。

hanyel 2009-03-15 06:54:00
建模如下:
表TABLE 内有 A B 字段
A是唯一的,B是不唯一的。

用SQL语句返回表内的数据,如果B有2次的,只出现第一条。示例如下
A B
1 a
2 a
3 b
4 c
5 c
6 c

返回如下的结果
A B
1 a
3 b
4 c


谢谢!
...全文
111 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
select A=min(A),B
from tb
group by B
ai_li7758521 2009-03-15
  • 打赏
  • 举报
回复
if object_id('Test') is not null drop table Test

create table Test(A int,B varchar(20))

insert Test
select 1,'a' union all
select 2,'a' union all
select 3,'b' union all
select 4,'c' union all
select 5,'c' union all
select 6,'c'


select a.*
from Test a
where a.A=(select min(b.A) from Test b Group by b.B having a.B=b.B)
sdhdy 2009-03-15
  • 打赏
  • 举报
回复
select min(a) a ,b from tb group by b
ks_reny 2009-03-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ks_reny 的回复:]
SQL code
select min(a.A) as A,b.B From a,b
where a.id=b.id
group by b.B
[/Quote]
不好意思,看错了,原来是一个表。

select min(a),b from A group by b
水族杰纶 2009-03-15
  • 打赏
  • 举报
回复
declare @t table(A int, B varchar(10)) 
insert @t select 1, 'a'
insert @t select 2, 'a'
insert @t select 3, 'b'
insert @t select 4, 'c'
insert @t select 5, 'c'
insert @t select 6, 'c'
select * from @t t where not exists(select 1 from @t where b=t.b and a<t.a)
/*A B
----------- ----------
1 a
3 b
4 c

(3 行受影响)*/
水族杰纶 2009-03-15
  • 打赏
  • 举报
回复
select max(a)a ,b from tb group by b
ks_reny 2009-03-15
  • 打赏
  • 举报
回复

select min(a.A) as A,b.B From a,b
where a.id=b.id
group by b.B

22,181

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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