22,210
社区成员
发帖
与我相关
我的任务
分享
现在有张表数据是这样的
==================源表======================
v1 v2 v3 v_sum
芯料类 制芯辅料 石英沙 2
芯料类 制芯辅料 石英沙 4
芯料类 制芯辅料 木积土 5
芯料类 制芯辅料 木积土 2
芯料类 制芯原料 鹅蛋石 1
芯料类 制芯原料 鹅蛋石 1
现在要插入列合计
===================结果表====================
芯料类 制芯辅料 石英沙
芯料类 制芯辅料 石英沙
合计(石英沙) 6
芯料类 制芯辅料 木积土
芯料类 制芯辅料 木积土
合计(木积土) 7
合计(辅料) 13
芯料类 制芯原料 鹅蛋石
芯料类 制芯原料 鹅蛋石
合计(鹅蛋石) 2
合计(原料) 2
合计(芯料) 15
==========================================
现在没有一点思路... 不知道插入的位置... 请教高手. 在线等~~~
白杆I 101-2B一头坏
白杆I 101--2H副牌
白杆I 101-4B7.2六角副牌
白杆I 101--4B副牌
白杆I 101--4H副牌
白杆I 101-2B一头坏
白杆I 101-2B一头坏 NULL
白杆I 101-2B一头坏 NULL NULL
白杆I 101-2B一头坏 NULL NULL NULL
白杆I 101-2B一头坏 NULL NULL NULL NULL
白杆I 101--2H副牌
白杆I 101--2H副牌 NULL
白杆I 101--2H副牌 NULL NULL
白杆I 101--2H副牌 NULL NULL NULL
白杆I 101--2H副牌 NULL NULL NULL NULL
DECLARE @t TABLE(Groups char(2),Item varchar(10),Color varchar(10),Quantity int)
INSERT @t SELECT 'aa','Table','Blue', 124
UNION ALL SELECT 'bb','Table','Red', -23
UNION ALL SELECT 'bb','Cup' ,'Green',-23
UNION ALL SELECT 'aa','Chair','Blue', 101
UNION ALL SELECT 'aa','Chair','Red', -90
--汇总显示
SELECT Groups=CASE
WHEN GROUPING(Color)=0 THEN Groups
WHEN GROUPING(Groups)=1 THEN '总计'
ELSE '' END,
Item=CASE
WHEN GROUPING(Color)=0 THEN Item
WHEN GROUPING(Item)=1 AND GROUPING(Groups)=0 THEN Groups+' 合计'
ELSE '' END,
Color=CASE
WHEN GROUPING(Color)=0 THEN Color
WHEN GROUPING(Color)=1 AND GROUPING(Item)=0 THEN Item+' 小计'
ELSE '' END,
Quantity=SUM(Quantity)
FROM @t
GROUP BY Groups,Item,Color WITH ROLLUP
/*--结果
Groups Item Color Quantity
-------- ---------------- ---------------------- -----------
aa Chair Blue 101
aa Chair Red -90
Chair 小计 11
aa Table Blue 124
Table 小计 124
aa 合计 135
bb Cup Green -23
Cup 小计 -23
bb Table Red -23
Table 小计 -23
bb 合计 -46
总计 89
--*/
--------------------------
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2009/03/22/4014856.aspx
create table #t(a int,b int,c int,d int,e int)
insert into #t values(1,2,3,4,5)
insert into #t values(1,2,3,4,6)
insert into #t values(1,2,3,4,7)
insert into #t values(1,2,3,4,8)
insert into #t values(1,3,3,4,5)
insert into #t values(1,3,3,4,6)
insert into #t values(1,3,3,4,8)
insert into #t values(1,3,3,4,7)
insert into #t values(2,2,2,4,5)
insert into #t values(2,2,3,4,6)
insert into #t values(2,2,4,4,7)
insert into #t values(2,2,5,4,8)
insert into #t values(2,3,6,4,5)
insert into #t values(2,3,3,4,6)
insert into #t values(2,3,3,4,8)
insert into #t values(2,3,3,4,7)
select
case when grouping(a)=1 then '合计' else cast(a as varchar) end a, b, sum(c) as c,sum(d) as d,sum(e) as e
from
#t
group by
a,b
with rollup
having grouping(b)=0 or grouping(a)=1
select
case when grouping(a)=1 then '合计' else cast(a as varchar) end a, b, c, sum(d) as d,sum(e) as e
from
#t
group by
a,b,c
with rollup
having grouping(c)=0 or grouping(a)=1
select
case when grouping(a)=1 then '合计' else cast(a as varchar) end a, b,
case when grouping(c)=1 and grouping(b)=0 then '小计' else cast(c as varchar) end c,
sum(d) as d,sum(e) as e
from
#t
group by
a,b,c
with rollup
having grouping(a)=1 or grouping(b)=0
select
case when grouping(a)=1 then '合计' else cast(a as varchar) end a,
case when grouping(b)=1 and grouping(a)=0 then '小计' else cast(b as varchar) end b, sum(c) as c, sum(d) as d,sum(e) as e from #t
group by
a,b,c
with rollup
having grouping(a)=1 or grouping(b)=1 or grouping(c)=0
drop table #t
/*a b c d e
------------------------------ ----------- ----------- ----------- -----------
1 2 12 16 26
1 3 12 16 26
2 2 14 16 26
2 3 15 16 26
合计 NULL 53 64 104
(所影响的行数为 5 行)
a b c d e
------------------------------ ----------- ----------- ----------- -----------
1 2 3 16 26
1 3 3 16 26
2 2 2 4 5
2 2 3 4 6
2 2 4 4 7
2 2 5 4 8
2 3 3 12 21
2 3 6 4 5
合计 NULL NULL 64 104
(所影响的行数为 9 行)
a b c d e
------------------------------ ----------- ------------------------------ ----------- -----------
1 2 3 16 26
1 2 小计 16 26
1 3 3 16 26
1 3 小计 16 26
2 2 2 4 5
2 2 3 4 6
2 2 4 4 7
2 2 5 4 8
2 2 小计 16 26
2 3 3 12 21
2 3 6 4 5
2 3 小计 16 26
合计 NULL NULL 64 104
(所影响的行数为 13 行)
a b c d e
------------------------------ ------------------------------ ----------- ----------- -----------
1 2 12 16 26
1 3 12 16 26
1 小计 24 32 52
2 2 2 4 5
2 2 3 4 6
2 2 4 4 7
2 2 5 4 8
2 3 9 12 21
2 3 6 4 5
2 小计 29 32 52
合计 NULL 53 64 104
(所影响的行数为 11 行)
*/