请教字段数据重复问题

glge 2006-02-11 10:39:58
表test1
字段 ID a b
1 aaa dsd
2 aaa dsada
3 aaa fdfdsfs
4 bb wqeqwe
5 hhh ewqed
6 hhh ewqe
7 uu ewqe
希望的到字段a中每个重复个记录的第一个数据,同时得到所对应字段b中的相应数据,按顺序排序
也就是得到结果为如下的数据集

字段 ID a b
1 aaa dsd
4 bb wqeqwe
5 hhh ewqed
7 uu ewqe

请教如何实现?
...全文
105 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxzm1001 2006-02-12
  • 打赏
  • 举报
回复
declare @t table
(ID int,a varchar(20),b varchar(20))

insert @t

select 1,'aaa','dsd' union all
select 2,'aaa','dsada' union all
select 3,'aaa','fdfdsfs' union all
select 4,'bb','wqeqwe' union all
select 5,'hhh','ewqed' union all
select 6,'hhh','ewqe' union all
select 7,'uu','ewqe'

select * from @t where id in(select min(id) from @t group by a)
glge 2006-02-11
  • 打赏
  • 举报
回复
谢谢。。
不过还是没有解决问题
补充一下
表中的数据很多,需要动态的得到结果,可以说事先并不知道表中有什么数据,所以只能动态的判断。
应该怎么解决呢
samfeng_2003 2006-02-11
  • 打赏
  • 举报
回复
declare @t table
(ID int,a varchar(20),b varchar(20))

insert @t

select 1,'aaa','dsd' union all
select 2,'aaa','dsada' union all
select 3,'aaa','fdfdsfs' union all
select 4,'bb','wqeqwe' union all
select 5,'hhh','ewqed' union all
select 6,'hhh','ewqe' union all
select 7,'uu','ewqe'

select * from @t a
where not exists (select * from @t b where a.a=b.a and a.id>b.id)

ID a b
----------- -------------------- --------------------
1 aaa dsd
4 bb wqeqwe
5 hhh ewqed
7 uu ewqe

(所影响的行数为 4 行)

22,301

社区成员

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

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