求一个sql语句,在线等,成功就给分。
表1:table_res
id name ......//列名
one name1
two name2
three name3
表2:table_value
id time value //列名
three 2013-09-16 10:55 33%
one 2013-09-16 10:56 22%
two 2013-09-16 09:55 35%
three 2013-09-16 11:53 43%
two 2013-09-16 10:38 33%
one 2013-09-16 12:55 17%
我想要的效果就是
id time value //列名
three 2013-09-16 11:53 43%
one 2013-09-16 12:55 17%
two 2013-09-16 10:38 33%
即id 不能重复,而且time为离现在时间最近的time,value 查出来。
求高手帮忙写个sql语句,谢谢了。
------解决方案--------------------
with table_res as
(
select 'one' id ,'name1' name from dual union all
select 'two' id ,'name2' name from dual union all
select 'three' id ,'name3' name from dual
),table_value as
(
select 'three' id ,'2013-09-16 10:55' time,'33%' value from dual union all
select 'one' id ,'2013-09-16 10:56' time,'22%' value from dual union all