34,576
社区成员
发帖
与我相关
我的任务
分享
select *,(select sum(当天收款) from tab where 公司名=a.公司名 and 日期<=a.日期) as [当天累计总额]
from tab a
order by 公司名,日期
为了抢,未测试,以下是测试的.
create table 表(公司名 varchar(50),日期 varchar(50),当天收款 int)
insert into 表 select 'A','20090201',100
insert into 表 select 'A','20090202',120
insert into 表 select 'B','20090201',50
insert into 表 select 'B','20090205',60
insert into 表 select 'C','20090202',30
SELECT * , 当天累计总额 = (SELECT SUM(当天收款) FROM 表 where 公司名 = a.公司名 and 日期 <= a.日期) FROM 表 a
SELECT *,当天累计总额=(SELECT SUM(当天收款) FROM 表 WHERE 公司名 <= a.公司名) FROM 表 a
如果这句不行,用这句:
SELECT *,当天累计总额=(SELECT SUM(当天收款) FROM 表 WHERE 公司名 >= a.公司名) FROM 表 a
if object_id('tb') is not null
drop table tb
go
create table tb(公司名 varchar(50),日期 varchar(50),当天收款 int)
insert into tb select 'A','20090201',100
insert into tb select 'A','20090202',120
insert into tb select 'B','20090201',50
insert into tb select 'B','20090205',60
insert into tb select 'C','20090202',30
select *,
当天累计总额=(select sum(当天收款) from tb where 公司名=t.公司名 and cast(日期 as datetime)<=cast(t.日期 as datetime)) from tb t
select *,(select sum(当天收款) from tab where 公司名=a.公司名 and 日期<=a.日期) as [当天累计总额]
from tab a
order by 公司名,日期
select *,(select sum(当天收款) from tab where 公司名=a.公司名 and 日期<=a.日期) as [当天累计总额]
from tab a
select *,
当天累计总额=(select sum() from tb where 公司名=t.公司名 and cast(日期 as datetime)<=cast(t.日期 as datetime))
from tb t