请教大家,用什么sql语句可以生成我希望生成的表
stock表,结构与记录如下:
日期(rdate) 数量(qty) 进出(dc)
4/1 40 1
4/1 50 1
4/5 20 -1
4/15 10 1
4/20 20 -1
希望生成这样的表
日期 余额
4/1 90
4/2 90
4/3 90
4/4 90
4/5 70
4/6 70
...
4/14 70
4/15 80
4/16 80
...
4/20 60
...
4/30 60
[解决办法]
stock表,结构与记录如下:日期(rdate) 数量(qty) 进出(dc)4/1 40 14/1 50 14/5 20 -14/15 10 14/20 20 -1希望生成这样的表日期 余额4/1 904/2 904/3 904/4 904/5 704/6 70...4/14 704/15 804/16 80...4/20 60...create table stock(rdate char(10),qty int,dc int)insert into stock values ('2012-04-01',40,1)insert into stock values ('2012-04-01',50,1)insert into stock values ('2012-04-05',20,-1)insert into stock values ('2012-04-15',10,1)insert into stock values ('2012-04-20',20,-1)select date as rdate,qty=(select sum(qty*dc) from stock where rdate<=a.date) from(select convert(char(10),dateadd(day,number,'2012-04-01'),23) as date from master.dbo.spt_values where type='P' and number<=30)a ----结果rdate qty2012-04-01 902012-04-02 902012-04-03 902012-04-04 902012-04-05 702012-04-06 702012-04-07 702012-04-08 702012-04-09 702012-04-10 702012-04-11 702012-04-12 702012-04-13 702012-04-14 702012-04-15 802012-04-16 802012-04-17 802012-04-18 802012-04-19 802012-04-20 602012-04-21 602012-04-22 602012-04-23 602012-04-24 602012-04-25 602012-04-26 602012-04-27 602012-04-28 602012-04-29 602012-04-30 602012-05-01 60