读书人

求一条取最大值的SQL话语

发布时间: 2013-06-19 10:26:41 作者: rapoo

求一条取最大值的SQL语句
单号 流水
A 1
A 2
A 3
B 1
B 2
C 1


现在想写个语句 只列出每个单号的流水最大的那条记录

A 3
B 2
C 1
[解决办法]

select 单号,max( 流水)流水
from tb
group by 单号


+++++++++1
[解决办法]
select *
from tb a
where exists (select 1 from (select 单号,max( 流水)流水
from tb
group by 单号 ) b where a.单号=b.单号 and a.流水
=b.流水
)
[解决办法]

select * from tb t where is not exists(
select 1 from tb where 单号=t.单号 and 流水>t.流水
)

读书人网 >SQL Server

热点推荐