分类汇总的SQL语句

plutu 2012-01-04 09:16:16
这是一个成本核算表,编码为4为,第一位为大类,第二位为子类,三四位是明细,我现在想用简单的语句实现明细金额汇总到子类的金额中,子类金额汇总到大类金额中,然后将各个大类金额汇总到总成本中,请问在SQL Server2K中SQL语句如何写?谢谢!
表结构及记录示例如下:
CREATE TABLE [dbo].[test] (
[编码] [char] (4) ,
[名称] [char] (20),
[金额] [decimal](9, 2))
GO

insert into test
select '1000 ','一、可控成本',0 union all
select '1100 ',' (1)、原材料',0 union all
select '1101 ',' 钢材',100 union all
select '1102 ',' 铜材',200 union all
select '1103 ',' 铝材',300 union all
select '1200 ',' (2)、辅助材料',0 union all
select '1201 ',' 轴承',400 union all
select '1202 ',' 润滑油',500 union all
select '1300 ',' (3)、燃料动力',0 union all
select '1301 ',' 电费',400 union all
select '1302 ',' 水费',500 union all
select '1303 ',' 水费',500 union all
select '2000 ','二、固定成本',0 union all
select '2100 ',' 人工',600 union all
select '2101 ',' 折旧',700 union all
select '2102 ',' 其它',200 union all
select '0000 ','总成本',0
...全文
347 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongsheng10101 2012-01-10
  • 打赏
  • 举报
回复
那咱来接分吧。

char与varchar注意区别应用,
如你的[编号]字段,是主键,都是4位的,用char,
[名称]字段,数据值不一定是20个字符,这时应用varchar类型。
勿勿 2012-01-05
  • 打赏
  • 举报
回复
LZ自己搞定了 ,恭喜恭喜!!
dawugui 2012-01-05
  • 打赏
  • 举报
回复
应该是逐级汇总吧?可参考如下:

/*
标题:查询指定节点及其所有子节点的函数
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2009-10-20
地点:陕西西安
地址:http://topic.csdn.net/u/20091020/18/a667c3dd-07e4-414d-9200-06c35d3d31a9.html
*/
ID parentid(下级级ID) 名称 数量
1 0 名称1 1
2 1 名称2 1
3 1 名称3 1
4 2 名称4 1
5 2 名称5 1
6 3 名称6 1
7 0 名称7 1
8 7 名称8 1
9 7 名称9 1

合计 6(名称1 以下所有节点 合计数量)
名称1 1
合计 3(名称2以下有节点)
名称2 1
名称4 1
名称5 1
合计 2
名称3 1
名称6 1



create table tb(ID int,parentid int, name varchar(10) ,cnt int)
insert into tb values(1 , 0 , '名称1' , 1)
insert into tb values(2 , 1 , '名称2' , 1)
insert into tb values(3 , 1 , '名称3' , 1)
insert into tb values(4 , 2 , '名称4' , 1)
insert into tb values(5 , 2 , '名称5' , 1)
insert into tb values(6 , 3 , '名称6' , 1)
insert into tb values(7 , 0 , '名称7' , 1)
insert into tb values(8 , 7 , '名称8' , 1)
insert into tb values(9 , 7 , '名称9' , 1)
go
--创建临时表
create table tmp (name varchar(10) ,cnt int)
go
--创建查询指定节点及其所有子节点的函数
create function f_cid(@ID int) returns @t_level table(id int , level int)
as
begin
declare @level int
set @level = 1
insert into @t_level select @id , @level
while @@ROWCOUNT > 0
begin
set @level = @level + 1
insert into @t_level select a.id , @level
from tb a , @t_Level b
where a.parentid = b.id and b.level = @level - 1
end
return
end
go

--创建存储过程并将数据插入临时表
create proc my_proc
as
begin
declare @id as int
declare @cnt as int
declare @name as varchar(10)
set @id = 0
while exists(select 1 from tb where id > @id)
begin
set @id = (select min(id) from tb where id > @id)
set @name = (select name from tb where id = @id)
set @cnt = (select sum(cnt) from (select a.* from tb a , f_cid(@id) b where a.id = b.id ) t)
insert into tmp select @name , @cnt
end
end
go
exec my_proc

select * from tmp

drop table tb , tmp
drop function f_cid
drop proc my_proc

/*
name cnt
---------- -----------
名称1 6
名称2 3
名称3 2
名称4 1
名称5 1
名称6 1
名称7 3
名称8 1
名称9 1

(所影响的行数为 9 行)

*/
dawugui 2012-01-05
  • 打赏
  • 举报
回复
恭喜.
plutu 2012-01-05
  • 打赏
  • 举报
回复
昨晚的论坛很不给力,直接就打不开
其实我的示例数据中有一个笔误,固定成本中没有小类,编码为‘2100’应该改为‘2103’,或者改成这样的测试语句:
CREATE TABLE [dbo].[test] (
[编码] [char] (4) ,
[名称] [char] (20),
[金额] [decimal](9, 2))
GO
insert into test
select '1000 ','一、可控成本',0 union all
select '1100 ',' (1)、原材料',0 union all
select '1101 ',' 钢材',100 union all
select '1102 ',' 铜材',200 union all
select '1103 ',' 铝材',300 union all
select '1200 ',' (2)、辅助材料',0 union all
select '1201 ',' 轴承',400 union all
select '1202 ',' 润滑油',500 union all
select '1300 ',' (3)、燃料动力',0 union all
select '1301 ',' 电费',400 union all
select '1302 ',' 水费',500 union all
select '1303 ',' 水费',500 union all
select '2000 ','二、固定成本',0 union all
select '2101 ',' 人工',600 union all
select '2102 ',' 折旧',700 union all
select '2103 ',' 其它',200 union all
select '0000 ','总成本',0

这样楼上老师的语句就得不出我要的结果了,不过我已经搞定了,请各位高手继续出些更高效的语句,谢谢,稍晚结贴
风一样的大叔 2012-01-05
  • 打赏
  • 举报
回复
-晴天 2012-01-04
  • 打赏
  • 举报
回复
CREATE TABLE [dbo].[test] (
[编码] [char] (4) ,
[名称] [char] (20),
[金额] [decimal](9, 2))
GO
insert into test
select '1000 ','一、可控成本',0 union all
select '1100 ',' (1)、原材料',0 union all
select '1101 ',' 钢材',100 union all
select '1102 ',' 铜材',200 union all
select '1103 ',' 铝材',300 union all
select '1200 ',' (2)、辅助材料',0 union all
select '1201 ',' 轴承',400 union all
select '1202 ',' 润滑油',500 union all
select '1300 ',' (3)、燃料动力',0 union all
select '1301 ',' 电费',400 union all
select '1302 ',' 水费',500 union all
select '1303 ',' 水费',500 union all
select '2000 ','二、固定成本',0 union all
select '2100 ',' 人工',600 union all
select '2101 ',' 折旧',700 union all
select '2102 ',' 其它',200 union all
select '0000 ','总成本',0
go
update a set 金额=(select SUM(金额) from test where LEFT(编码,2)=LEFT(a.编码,2)) from test a where a.编码 like '%00'
go
update a set 金额=(select SUM(金额) from test where LEFT(编码,1)=LEFT(a.编码,1) and 编码 like '__00') from test a where a.编码 like '_000'
go
update test set 金额=(select SUM(金额) from test where 编码 like '_000') where 编码='0000'
go
select * from test
/*
编码 名称 金额
---- -------------------- ---------------------------------------
1000 一、可控成本 2900.00
1100 (1)、原材料 600.00
1101 钢材 100.00
1102 铜材 200.00
1103 铝材 300.00
1200 (2)、辅助材料 900.00
1201 轴承 400.00
1202 润滑油 500.00
1300 (3)、燃料动力 1400.00
1301 电费 400.00
1302 水费 500.00
1303 水费 500.00
2000 二、固定成本 1500.00
2100 人工 1500.00
2101 折旧 700.00
2102 其它 200.00
0000 总成本 4400.00

(17 行受影响)

*/
go
drop table test

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧