询问一个SQL写法

zhuhua 2006-04-12 08:57:08
一个表的字段
D A B C
1 1 2 3
2 1 2 4
3 2 3 4
4 2 3 5
要求的效果
D A B C
1 1 2 3
3 2 3 4

其中D是主键,要求取A字段相同情况下的第一条
...全文
61 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
paoluo 2006-04-12
  • 打赏
  • 举报
回复
Select * from TableName A Where D In (Select TOP 1 D from TableName Where A=A.A Order By D)
撸大湿 2006-04-12
  • 打赏
  • 举报
回复
create table #tb(D int,A int,B int,C int)
insert into #tb
select 1,1,2,3 union all
select 2,1,2,4 union all
select 3,2,3,4 union all
select 4,2,3,5

select * from #tb a where
exists(select 1 from #tb where A=a.A and D>a.D)
drop table #tb
xeqtr1982 2006-04-12
  • 打赏
  • 举报
回复
declare @t table(D int,A int,B int,C int)
insert into @t select 1,1,2,3
union all select 2,1,2,4
union all select 3,2,3,4
union all select 4,2,3,5

select * from @t a where exists(select 1 from @t where A=a.A and D>a.D)

--这样吗?

34,576

社区成员

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

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