首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > IBM DB2 >

db2排序后再只取一条记录的有关问题

2012-07-19 
db2排序后再只取一条记录的问题select * from (select *from PAYHLSTwhere pay_no200902270001order by

db2排序后再只取一条记录的问题
select * from ( 
  select * 
  from PAYHLST 
  where pay_no='200902270001'
  order by hire_bgn_tm desc
  ) as v
fetch first 1 rows only 

这个写法为什么不能得到 hire_bgn_tm 最大的那条记录呀? 

实际得到的结果和 

select * from ( 
  select * 
  from PAYHLST 
  where pay_no='200902270001'
  ) as v
fetch first 1 rows only 

得到的一致,不知道为什么,是不是db2不支持?
环境是ibm as400的服务器。谢谢!!

[解决办法]
try:
select * from ( 
select * 
from PAYHLST 
where pay_no='200902270001'
) as v 
order by hire_bgn_tm desc 
fetch first 1 rows only 

[解决办法]
子查询排了序,外层查询没排序,结果就和没排序一样。
只用原SQL中的子查询就可以了:
select * 
from PAYHLST 
where pay_no='200902270001' 
order by hire_bgn_tm desc 
fetch first 1 row only

热点排行