您的位置:程序门 -> db2 -> 数据库开发



新人提问~谢谢大家.


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


新人提问~谢谢大家.
发表于:2007-08-22 11:21:42 楼主
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的选出来.这个怎么实现?谢谢大家!
发表于:2007-09-12 17:28:181楼 得分:0
select   max(id),hao,max(nf)   from   table1   group   by   hao

不知道你对id有什么要求,就显示最大的了
发表于:2007-09-14 13:24:152楼 得分:0
select   a.*   from   (select   row_number()   over(partition   by   hao   order   by   nf   desc)   num,
id,hao,nf   from   table1)a   where   a.num=1;
发表于:2007-09-14 16:40:143楼 得分:0
select   t.*   from   table1   t   where   not   exists(select   1   from   table1   where   hao=t.hao   and   nf> t.nf)
发表于:2007-09-14 16:41:364楼 得分:0
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


快速检索

热门点击