分类汇总的SQL语句

plutu 2012-01-04 09:19:58
这是一个成本核算表,编码为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
...全文
266 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶子 2012-01-30
  • 打赏
  • 举报
回复
samurai 2012-01-29
  • 打赏
  • 举报
回复
declare @t table([编码] [char](4),[名称] [char](20),[金额] [decimal](9,2))
insert into @t
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
;with cte as(select SUBSTRING([编码],1,2) [编码],max(cast([编码] as varbinary)) px from @t group by SUBSTRING([编码],1,2))
select a.*,px=(
(select px from cte where 编码=LEFT(a.[编码],2))+cast(a.[编码] as varbinary))
from @t a order by px
(17 行受影响)

(17 行受影响)
0000 总成本 0.00 0x3030303030303030
1000 一、可控成本 0.00 0x3130303031303030
1100 (1)、原材料 0.00 0x3131303331313030
1101 钢材 100.00 0x3131303331313031
1102 铜材 200.00 0x3131303331313032
1103 铝材 300.00 0x3131303331313033
1200 (2)、辅助材料 0.00 0x3132303231323030
1201 轴承 400.00 0x3132303231323031
1202 润滑油 500.00 0x3132303231323032
1300 (3)、燃料动力 0.00 0x3133303331333030
1301 电费 400.00 0x3133303331333031
1302 水费 500.00 0x3133303331333032
1303 水费 500.00 0x3133303331333033
2000 二、固定成本 0.00 0x3230303032303030
2100 人工 600.00 0x3231303232313030
2101 折旧 700.00 0x3231303232313031
2102 其它 200.00 0x3231303232313032
  • 打赏
  • 举报
回复
十六进制转十进制:
--函数实现方法
CREATE FUNCTION dbo.f_hex_dec(@s varchar(16))
RETURNS bigint
AS
BEGIN
--参数不得含'0'~'9'、'a'~'f'、'A'~'F'之外的任意字符(首尾空格除外),否则返回0
DECLARE @i int,@result bigint
SELECT @i=0,@result=0,@s=RTRIM(LTRIM(UPPER(REVERSE(@s))))
WHILE @i<LEN(@s)
BEGIN
IF SUBSTRING(@s,@i+1,1) not between '0' and '9' and SUBSTRING(@s,@i+1,1) not between 'A' and 'F'
BEGIN
SELECT @result=0
break
END
SELECT @result=@result+(CHARINDEX(SUBSTRING(@s,@i+1,1),'0123456789ABCDEF')-1)*POWER(16,@i),@i=@i+1
END
RETURN @result
END
GO
dongsheng10101 2012-01-10
  • 打赏
  • 举报
回复
重复提问啊,
哈哈,那快散分吧。
勿勿 2012-01-05
  • 打赏
  • 举报
回复
LZ最好考虑下每层汇总。
-晴天 2012-01-05
  • 打赏
  • 举报
回复
建议增加一条记录:
select '2000 ','二、固定成本',0 union all
select '2101 ',' 人工',600 union all
select '2102 ',' 折旧',700 union all
select '2103 ',' 其它',200 union all

select '2100 ',' 其他',0 union all

这样,能与前面格式的统一起来处理.
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
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20120104/21/cbe27ad8-f7be-4c85-8d90-623cce1588ad.html?32541
plutu 2012-01-05
  • 打赏
  • 举报
回复
由于昨晚论坛不给力,发重了

27,579

社区成员

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

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