SQL选取取最后一条记录
现有一表JShu: ID Num JShu
1 D123456 56
2 D123567 70
3 D789462 56
4 D765325 60
5 D789321 50
………………………………………………
我想在存储过程中显示: Num_1 JShu
D123 70
D789 50
D765 60
………………………………………………
也就是Num字段提取前4位分组,JShu字段取该组中最后一个数,有可能做到吗?
请各位高手帮帮忙
------解决方案--------------------楼主的意图可以这么理解
选取前4为Num相同的,ID号最大的记录
select left(Num, 4) Num_1, JShu from JShu a
where not exists (select 1 from JShu where left(a.Num, 4) = left(Num, 4) and ID > a.ID)
------解决方案-------------------- select left(Num,4) as Num_1, JShu
from tbl a
where not exists( select 1 from tbl where left(a.Num,4)=left(Num,4) and a.id <id)