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的数据,如果没有,也取它上一次的数据。
解决方案 »
- 新手求帮助。
- select ... where ... in ()的原理
- 安装oracle10G服务器端后无法登陆?即使用system/sys也(ORA-01017:invalid username/password;)
- 求Oracle 9208不停机或尽量短时间停机升级到11.1.0.7的方案
- 请问怎么可以修改orecle9的端口?
- 急:ora-12699本机服务内部错误,愿呈上100分
- 这样的SQL语句怎么写?大家进来看看呀!
- 怎样将Oracle中的数据导出为sql脚本,急~~
- 新手遇到的问题:Oracle HTTP服务器启动错误:failed to create FastCGI application accept mutex
- oracle ogg for windows32
- 在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'