求一sql

mingl11 2010-11-15 09:50:41
现有一表,结构如下:

a b
nnn 855
nnn 986
eee 123
rrr 485
eee 598
ttt 625
eee 256
rrr 186

现在我想a列相同的只得到最后一行的。 也就是只得到蓝色的行
...全文
168 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mingl11 2010-11-16
  • 打赏
  • 举报
回复
多谢各位解答 。结贴
chen_ya_ping 2010-11-15
  • 打赏
  • 举报
回复

create table #t
(
id int not null identity(1,1) primary key,
a varchar(20) not null,
b varchar(20) not null
)

insert #t
values
('nnn','855'),
('nnn','986'),
('eee','123'),
('rrr','485'),
('eee','598'),
('ttt','625'),
('eee','256'),
('rrr','186')

select a,b from #t
where id in
(
select max(id) from #t
group by a
)

drop table #t

/*
the result is:
nnn 986
ttt 625
eee 256
rrr 186
*/
TearsStoneJava 2010-11-15
  • 打赏
  • 举报
回复
select distinct a,b from table where time
xinshouno7 2010-11-15
  • 打赏
  • 举报
回复
with table as
(
select row_number() over(order by getdate()) as id,a,b from table_1
)
select a.a,a.b
from table as a
where a.id = (select max(b.id) from talbe as b where a.a = b.a group by b.a )

5楼的可以,不过插入的时候需要注意先后顺序啊
zthsn 2010-11-15
  • 打赏
  • 举报
回复
学习下...
挨踢直男 2010-11-15
  • 打赏
  • 举报
回复
with temp as
(
select *,row_number() over( order by getdate()) id from test
)

select a,b from temp a where id = (select max(id) from temp where a=a.a )

hao1hao2hao3 2010-11-15
  • 打赏
  • 举报
回复

if object_id('tb')>0
drop table tb
create table tb
(
a varchar(10),
b int
)
go
insert into tb
select 'nnn', 855
union all
select 'nnn', 986
union all
select 'eee', 123
union all
select 'rrr', 485
union all
select 'eee', 598
union all
select 'ttt', 625
union all
select 'eee', 256
union all
select 'rrr', 186


with cte as
(
select row_number() over(order by getdate()) as id,a,b from tb
)
select a.a,a.b
from cte as a
where a.id = (select max(b.id) from cte as b where a.a = b.a group by b.a )


--结果

nnn 986
ttt 625
eee 256
rrr 186


--这是SQL Server2005才有的写法,2000 的话也要自己先构造一个id列。标示你的记录的顺序。

挨踢直男 2010-11-15
  • 打赏
  • 举报
回复
select * from tb a where id = (select max(id) from tb where a=a.a )

你那个表有没有id 有的话像上面那样写
没有的话不太好办
sth79 2010-11-15
  • 打赏
  • 举报
回复
学习。。。。收藏
wyq29 2010-11-15
  • 打赏
  • 举报
回复
select a, max(b) from T group by a
sweetqueen1 2010-11-15
  • 打赏
  • 举报
回复
先根據B列排序
select distinct a,b from table order by b desc
ASPNETCHENGXU 2010-11-15
  • 打赏
  • 举报
回复
如果就a,b 两个字段就是, select a,max(b) from t group by a
否则
select * from table1 t where 字段=(slect max(字段) from t.分组字段=分组字段 )

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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