Sign-up....

求助一较复杂SQL语句

有两个表tbl_200504,tbl_200505,结构一样

tbl_200504表 ,该表中的数据截止到200504,即截止到表名的最后六位

time num

200502 20

200503 20

200504 30

tbl_200505表,该表中的数据截止到200505

time num

200502 20

200503 10

200504 30

200505 100

想得到如下结果

time takeback_num

200505 10

这个10 是表tbl_200504中time截止到200504以前的num之和减去表tbl_200505中time截止到200504以前的num之和

即:(20+20+30)-(20+10+30)=10

[341 byte] By [msdn] at [2007-10-25 21:51:04]
# 1 Re: 求助一较复杂SQL语句

select '200505' time,(select sum(num) from tbl_200504 where time<='200504')-(select sum(num) from tbl_200505 where time<='200504') num from dual;

bzszp at 2005-7-4 11:01:13 >
# 2 Re: 求助一较复杂SQL语句

select a.sum1-b.sum2 from

(select sum(num) sum1 from tbl_200504 where time<=to_date('200504','yyyymm)) a,

(select sum(num) sum2 from tbl_200505 where time<to_date('200505','yyyymm)) b;

wfeng7907 at 2005-7-4 11:08:52 >
# 3 Re: 求助一较复杂SQL语句

select (select max(time) from tbl_200505) as thismonth,

sum(a.num)-sum(b.num) from tbl_200505 a,tbl_200504 b

where a.time<=(select max(time) from tbl_200505)

and b.time <(select max(time) from tbl_200505)

heyixiang at 2005-7-4 12:15:05 >

Oracle

All Classified