分類匯總問題
drop table acreate table a (a int ,b int,c int)insert into a
select 1,100,10 union all
select 2,200 ,20 union all
select 3,300,30 union all
select 4,300,40 union all
select 5,400,50 union all
select 6,500,60 union all
select 7,700,70 union all
select 8,800,80select * from a
a b c
1 100 10
2 200 20
3 300 30
4 300 40
5 400 50
6 500 60
7 700 70
8 800 80
---要實現newc=對上一列的值加上本身,CsumB=C+B(--C,B為對上一列的值加上本身)
a b c newc CsumB
1 100 10 10 110
2 200 20 30 330
3 300 30 60 660
4 300 40 100 1000
5 400 50 150 1450
6 500 60 210 2010
7 700 70 280 2780
8 800 80 360 3660
select identity(int,1,1) as id,* into #t from a---加上標志字段
select a.a,a.b,a.c,newC=(select sum(c) from #t where id<=a.id),CsumB=(select sum(c+b) from #t where id<=a.id) from #t a
---結果
--刪除測試條件
drop table # t
drop table acreate table a (a int ,b int,c int)insert into a
select 1,100,10 union all
select 2,200 ,20 union all
select 3,300,30 union all
select 4,300,40 union all
select 5,400,50 union all
select 6,500,60 union all
select 7,700,70 union all
select 8,800,80select * from a
a b c
1 100 10
2 200 20
3 300 30
4 300 40
5 400 50
6 500 60
7 700 70
8 800 80
---要實現newc=對上一列的值加上本身,CsumB=C+B(--C,B為對上一列的值加上本身)
a b c newc CsumB
1 100 10 10 110
2 200 20 30 330
3 300 30 60 660
4 300 40 100 1000
5 400 50 150 1450
6 500 60 210 2010
7 700 70 280 2780
8 800 80 360 3660
select identity(int,1,1) as id,* into #t from a---加上標志字段
select a.a,a.b,a.c,newC=(select sum(c) from #t where id<=a.id),CsumB=(select sum(c+b) from #t where id<=a.id) from #t a
---結果
--刪除測試條件
drop table # t
解决方案 »
- 一个数据库问题,哪位高人帮忙解决阿
- 一个简单的触发器的写法 在线等
- 急求教 sql的删除列
- 请问各位大虾 windows xp系统支持sql server 吗
- 求 一条更新 语句
- 多次查询的数据集更新问题
- 用SQL如何仅仅得到表的结构!!
- 看这帖后,让我以为 Sql Server 是可以反删除的.
- 紧急求教! sql 2000 问题,请高手帮忙解决
- You have decided to perform an incomplete recovery of database, which
- 如何在同一张表里把昨天的数据全部增加进来并把日期改为今天。
- 看到别人的数据库中用户里面dbo的登录名不是sa,为什么呢?
from 表
group by 称重性质,供货单位
from 表
group by 称重性质,供货单位 with rollup
having grouping(称重性质)=0
供货单位=case when grouping(供货单位)=1 then ' 'else 供货单位 end ,
重量=sum(重量)
from 表
group by 称重性质,供货单位 with rollup
insert tb select 1,'AAAA','CC',10
union all select 2,'AAAB','CC',20
union all select 3,'SSSS','BC',15
union all select 4,'SDAA','DF',14
union all select 5,'AAAA','CC',5
go--查询
select 称重性质,供货单位=case when grouping(供货单位)=1 then '' else 供货单位 end,
重量=sum(重量)
from tb
group by 称重性质,供货单位 with rollup
having grouping(称重性质)=0
order by 称重性质,grouping(供货单位) desc,供货单位
go--删除测试
drop table tb/*--结果称重性质 供货单位 重量
---------- ---------- -----------
BC 15
BC SSSS 15
CC 35
CC AAAA 15
CC AAAB 20
DF 14
DF SDAA 14(所影响的行数为 7 行)
--*/