两张无关联表 根据时间这么累计金额?求SQL语句或解决方案

表A id datetime pay(收入表)
表B id Bdatetime pay(支出表)

A:
1 2012-08-19 22.00
2 2012-09-23 32.00
3 2012-09-30 33.00
B:
1 2012-08-20 -11.00
2 2012-09-24 -20.00
输出:
C:(id Cdatetime pay paycount)
1 2012-08-19 22.00 22.00
2 2012-08-20 -11.00 11.00
3 2012-09-23 32.00 43.00
4 2012-09-24 -20.00 23.00

一个sql语句应该无法完成。
建议使用TABLE函数实现。TABLE函数使用不负责。
例:

create or replace type t_test as object(
id integer,
rq date,
mc varchar2(60)
)
/
create or replace type t_test_table as table of t_test
/

create or replace function f_test_pipe(n in number default null) return t_test_table PIPELINED 
as 
v_test t_test_table := t_test_table();
begin 
for i in 1 .. nvl(n,100) loop
pipe row(t_test(i,sysdate,'mc'||i)); 
end loop; 
return; 
end f_test_pipe; 
/
select * from table(f_test_pipe(20))
/

请问下你用的是什么数据库?因为,所以只能给你一下我的看法!看你的意思应该是两个表之间关联关系是时间。
我想的办法:
1.直接查询两个表 然后用union 去重,获取不重复的日期。然后通过日期进行相关联查询
2.如果你用oracle的话,可以尝试用下完外连接来得到你想要的结果集

select a.*,(

select SUM(pay) from(

select * from A
union all
select * from B
) b where b.datetime<=a.datetime
) paycount
from
(
select * from A
union all
select * from B) a order by a.datetime ##