想建一视图,实现如下功能,不知如何做
表中有一字段num,希望在视图中新增一字段,得到该条记录及它前面的所有记录的sum(num)值
------解决方案--------------------表必须有ID列:
create view vTest
as
select *,
[sum] = (select sum(num) from table where id < a.id)
from table as a
------解决方案--------------------如果界定排在当前记录前面的?
ID?还是时间?
--用子查询: <=
create view vTest
as
select *,
(select sum(num) from 表名 where id <= a.id) as [sum]
from 表名 as a