A表数据如下:
order date
80010021 2015/4/13
80010022 2015/2/12
B表数据如下:
order date name
80010021 2015/2/1 H150
80010021 2015/3/12 H180
80010021 2015/3/1 H190
80010022 2009/1/3 SBT240
80010022 2012/5/3 SBT108.1
80010022 2014/9/1 SUT9980
要取A表中order相同,但date大于B表date,B表中对应的最大一条记录,结果如下:
order date date1 name
80010021 2015/4/13 2015/3/12 H180
80010022 2015/2/12 2014/9/1 SUT9980
因为对oracle不熟,麻烦帮助一下,谢谢!
------解决思路----------------------
select t2.order,t2.date,t2.max_dt,t1.name from b t1,
(select a.order,a.date,(select max(b.date) from b where a.order=a.order and a.date>b.date)max_dt from a)t2
where t1.order=t2.order
and t1.date=t2.date
------解决思路----------------------
字段名不能为order,date 啊
后面都加了一个数字1
SELECT order1, date1, date2, name1
FROM (SELECT a.order1,
a.date1,
b.date1 date2,
b.name1,
ROW_NUMBER() OVER(PARTITION BY a.order1 ORDER BY b.date1 DESC) rn
from a, b
where a.order1 = b.order1
and a.date1 > b.date1)
WHERE rn = 1;