解决方案 »
- FileInfo求教
- firefox里面一个表单提交问题
- 如何在DATALIST中以固定格示显示日期字段
- 高手 帮忙啊!~一个菜鸟的问题 分全给
- 虚拟主机 正在打开网页 http...." 而且持续时间很长?
- 初学:ASP.NET连接数据库以及数据类型的问题
- 在datagrid中设置邦定列的字段,但是数据源是含有两张表的DATASET,请问应该如何指定字段名?谢谢,在线等!
- 关网站上传,那位高手进来请教!高分求救!
- webChart
- ASP.NET 2.0中从数据库中读取数据到Label中的时候,为什么在网页中却显示不出来????
- SQL商品对应多个图片的问题
- 循环DropDownList二级级联前后台如何写?
select distinct a,b from table order by b desc
你那个表有没有id 有的话像上面那样写
没有的话不太好办
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列。标示你的记录的顺序。
(
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 )
(
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楼的可以,不过插入的时候需要注意先后顺序啊
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
*/
否则
select * from table1 t where 字段=(slect max(字段) from t.分组字段=分组字段 )