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

新人求问,没有多少分,刚来,多谢大家了

2012-02-21 
新人求问,没有多少分,刚来,谢谢大家了TABLE1TABLE2IDHaoNFIDHaoNF13200633200723200649200633200749200659

新人求问,没有多少分,刚来,谢谢大家了
TABLE1                                                   TABLE2

ID     Hao         NF                               ID     Hao         NF    
1         3           2006                           3         3           2007  
2         3           2006                           4         9           2006
3         3           2007
4         9           2006
5         9           2005

大家看懂了吧?我想从table1得到table2,以Hao为分类,每一类都把最大年份NF的选出来.这个怎么实现?谢谢大家!

[解决办法]
select a.* from table1 a,(select hao,max(nf) as nf from table1 group by hao) b where a.hao=b.hao and a.nf=b.nf
[解决办法]
可以用rownumber()函数

select * from 
(select a.*,rownumber() over(partition by Hao order by NF DESC) as order_id
 from TABLE1 a
) b
where order_id = 1

热点排行