首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

累加有关问题

2012-10-26 
累加问题SQL code---有这样一个表create table b(valuess int ,classfiy int ,companyid int)insert into

累加问题

SQL code
---有这样一个表create table b(valuess int ,classfiy int ,companyid int)insert into bselect 3,1,1union all select 4,1,1union all select 5,1,1union all select 5,2,1union all select 7,2,1union all select 5,2,1union all select 5,3,1union all select 7,3,1union all select 8,3,1union all select 4,1,2union all select 5,1,2union all select 5,2,2union all select 7,2,2union all select 5,2,2union all select 5,3,2union all select 7,3,2union all select 8,3,2----运行了这下面段出结果select SUM(valuess)vvvvv from b ygroup by companyid,classfiyorder by companyid


vvvvv
----------- 我相要的结果:vvvvv 
12 12
17 29
20 49
9 9
17 26
20 46

(6 row(s) affected) 就是将classfiy的小计再累加


[解决办法]
select vvvvv=(select SUM(valuess) from b 
where companyid=y.companyid and classfiy<=y.classfiy) 
 from b y
group by companyid,classfiy
order by companyid

热点排行