我的账户中有以下数据:
T_ID T_RESTDATE T_RESTSUM T_INCOME T_OUTCOME
1135782 20.04.2013 16714,31 16714,31 0
1135782 20.05.2013 33362,4 16648,09 0
1135782 20.06.2013 49179,59 15817,19 0
1135782 20.07.2013 64207,42 15027,83 0
1135782 20.08.2013 78485,35 14277,93 0
1135782 20.09.2013 92050,89 13565,54 0
1135782 20.10.2013 104939,65 12888,76 0
要生成日期之间的范围,我通常使用以下查询:
SELECT :dateBeg+level-1 turndate
FROM dual
CONNECT BY level <= :dateEnd-:dateBeg+1
如何获得以下结果:
T_ID T_RESTDATE T_RESTSUM T_INCOME T_OUTCOME
1135782 20.04.2013 16714,31 16714,31 0
1135782 21.04.2013 16714,31 16714,31 0
1135782 22.04.2013 16714,31 16714,31 0
1135782 23.04.2013 16714,31 16714,31 0
...
1135782 20.05.2013 33362,4 16648,09 0
1135782 21.05.2013 33362,4 16648,09 0
1135782 22.05.2013 33362,4 16648,09 0
...
答案 0 :(得分:0)
以下是两种方式。
一种糟糕的方式(但有时需要):
select *
from TABLE_NAME
where TABLE_NAME.DATE in (select :DATEBEG + level - 1 TURNDATE
from DUAL
connect by level <= :DATEEND - :DATEBEG + 1)
order by TABLE_NAME.DATE asc;
一个好方法:
select *
from TABLE_NAME
where TRUNC(TABLE_NAME.DATE) between TRUNC(TO_DATE(:DATEBEG)) and TRUNC(TO_DATE(:DATEEND))
order by TABLE_NAME.DATE asc;
答案 1 :(得分:0)
您可以生成一组从最小值到最大值的日期,然后将其与表格外部连接。 然后,使用LAST_VALUE函数填充空值。
create table mytab(
t_id number,
t_restdate date,
t_restsum varchar2(20)
);
insert into mytab values(1135782, date'2013-04-20', '16714,31');
insert into mytab values(1135782, date'2013-05-20', '33362,4');
insert into mytab values(1135782, date'2013-06-20', '49179,59');
查询1 :
with x(min_date, max_date) as (
select min(t_restdate), max(t_restdate)
from mytab
),
y(all_date) as (
select min_date + level - 1
from x
connect by min_date + level - 1 <= max_date
)
select last_value(mytab.t_id) ignore nulls over (order by y.all_date),
y.all_date,
last_value(mytab.t_restsum) ignore nulls over (order by y.all_date)
from y left outer join mytab
on y.all_date = mytab.t_restdate
order by y.all_date;
<强> Results 强>