`

一条sql语句

阅读更多


表a
no
111 
112
113

表b
no  year count detail
111 2009 1     hello
111 2010 3     nice

113 2010 2

从a,b取数据,b取最大年份数据,生成结果如下,:

表C
no   count  detail
111  3      nice
112  0
113  2


select a.no,c.year,c.cnt,c.detail
from a,(select * from b bb
where not exists(select 1 from  b tb where bb.no=tb.no
                                      and bb.year<tb.year)) c
where a.no=c.no(+)


      NO       YEAR        CNT DETAIL
-------- ---------- ---------- ----------
     111       2010          3 nice
     113       2010          2
     112



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics