作业帮 > 综合 > 作业

用oracle的话,查询语句该怎么写?

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/14 05:17:30
用oracle的话,查询语句该怎么写?
表中相关的字段有,beginDate,endDate,interest_rate三个,
起始时间至结束时间这一时间段内,对应一个值,前两个字段是char(8)型的,
对应日期的八个数字'yyyymmdd' 如:
id begin_date end_date interest_rate
----------- ---------- -------- ---------------------------------------
1 20130101 20130104 5.00000000
2 20130105 20130110 3.00000000
3 20130111 20130113 6.00000000
4 20130114 20130120 2.00000000
5 20130121 20130128 9.00000000
6 20130129 20130220 4.00000000
假设给定一个起始日期和结束日期,我需要查询这之间对应的每一天的interest_rate,
比如查询20130103到20130115,每天对应的值,查询结果如:
date_01 INTEREST_RATE
--------------------------------- -----------------------------
2013-01-03 00:00:00.000 5.00000000
2013-01-04 00:00:00.000 5.00000000
2013-01-05 00:00:00.000 3.00000000
2013-01-06 00:00:00.000 3.00000000
2013-01-07 00:00:00.000 3.00000000
2013-01-08 00:00:00.000 3.00000000
2013-01-09 00:00:00.000 3.00000000
2013-01-10 00:00:00.000 3.00000000
2013-01-11 00:00:00.000 6.00000000
2013-01-12 00:00:00.000 6.00000000
2013-01-13 00:00:00.000 6.00000000
2013-01-14 00:00:00.000 2.00000000
2013-01-15 00:00:00.000 2.00000000
Oracle 应该怎么写查询语句,
创建测试表
create table test
(id int,
begin_date char(8),
end_date char(8),
interest_rate int);

insert into test values (1,'20130101','20130104',5);
insert into test values (2,'20130105','20130110',3);
insert into test values (3,'20130111','20130113',6);
insert into test values (4,'20130114','20130120',2);
执行
with t as
(select to_char(to_date('20130103','yyyymmdd')+rownum-1,'yyyymmdd') as tdate from dual connect by rownum<50)
select t.tdate,test.interest_rate from t,test 
where t.tdate between '20130103' and '20130115' and t.tdate between test.begin_date and test.end_date
结果