TT1name rdate rodate price1 vdate vodate price2A 2008-02-03 2008-04-03 2 2008-02-03 2008-04-03 1A 2008-04-03 2008-05-23 4 2008-04-03 3
A 2008-05-23 2008-06-03 6
A 2008-06-03 8TT2name pdate podate price3 A 2008-01-01 13
要查询结果为
name price1 price2 price3 A 6 3 13 条件是查询TT1表里时间位于2008-05-23 到 2008-06-03里的price1的数据,同时取price2的数据,如果为空,就取它上一次的数据,和TT2关连也是取这个时间段的price3的数据,如果没有,也取它上一次的数据。
A 2008-05-23 2008-06-03 6
A 2008-06-03 8TT2name pdate podate price3 A 2008-01-01 13
要查询结果为
name price1 price2 price3 A 6 3 13 条件是查询TT1表里时间位于2008-05-23 到 2008-06-03里的price1的数据,同时取price2的数据,如果为空,就取它上一次的数据,和TT2关连也是取这个时间段的price3的数据,如果没有,也取它上一次的数据。
解决方案 »
- ▲请问这个SQL语句该怎么写,谢谢各位了!(有答案立刻结分)
- 求助oracle字符集转换
- 很简单的oracle问题谁帮我看看
- 求助:登录SQL PLUS时为何窗口关闭?
- (急!!!)我用sqlplus进行远程连接oracle时都能成功,但用Pro*C进行远程连接oracle时总连不上
- 请教:如何将oracle库中的表结构(表数量很大)生成表定义脚本文件,急(在线等)
- oracle9中不能显示新建数据库
- 请问哪有oracle下载?
- ASP+Orcale问题!老问新提关于日期的!高手进!
- 数据库提交问题?
- 在Oracle中查询的时候如何去掉重复列中的值呢
- 请教客户端Pro*C 程序连接Oracle的超时处理问题
如果为空,就按rdate rodate 的时间顺序取上一条记录的值?
tt1和tt2通过什么关联?如果按name关联的话,tt2中有多条name相同的值怎么办
tt2看着像是tt1的一部分..
union all select 'A','2008-04-03','2008-05-23',4,'2008-04-03',null,3 from dual
union all select 'A','2008-05-23','2008-06-04',6,null,null,null from dual
union all select 'A','2008-06-03',null,8,null,null,null from dual)
,tt2 as(select 'A' name,'2008-01-01' pdate,null podate,13 price3 from dual)select * from(
select tt1.name,tt1.rodate,decode(tt1.price1,null,lag(tt1.price1)over(order by
case when tt1.rdate<='2008-05-25' and (tt1.rodate>'2008-05-25' or tt1.rodate is null) then '2008-05-25' when tt1.price1 is null then '2020' else tt1.rdate end ),tt1.price1)price1,
decode(tt1.price2,null,lag(tt1.price2)over(order by
case when tt1.rdate<='2008-05-25' and (tt1.rodate>'2008-05-25' or tt1.rodate is null) then '2008-05-25' when tt1.price2 is null then '2020' else tt1.rdate end ),tt1.price2)price2
,tt2.price3
from tt1,tt2
where tt1.name=tt2.name
and tt1.rdate<='2008-05-25')
where rodate>'2008-05-25'
用你的实际数据替换掉'2008-05-25'