不要用循环实现行汇总
时间:2011-11-10
来源:互联网
同时记录该笔汇总的明细ID号。
金额=单价*数量
ID 物品 物品料号 单价 数量
1 泵总成 9684544580 99 100000
2 泵总成 9684544580 98 100000
3 泵总成1 9684544580 97 100000
4 泵总成1 9684544580 100 100000
5 泵总成2 9684544580 50 100000
6 泵总成2 9684544580 49 100000
7 泵总成2 9684544580 3 100000
结果
物品 物品料号 金额 ID2
泵总成 9684544580 990000 1
泵总成 9684544580 980000 2
泵总成1 9684544580 970000 3
泵总成1 9684544580 1000000 4
泵总成2 9684544580 990000 5、6
泵总成2 9684544580 30000 7
大概的情况就是这样,之前用循环数据量很大就很慢。麻烦高手指点了!谢谢
作者: wangdg520 发布时间: 2011-11-10
declare @t table(id int,物品 nvarchar(100),物品料号 nvarchar(100),单价 int ,数量 int); insert into @t select 1,'泵总成', '9684544580' ,99 ,100000 union all select 2,'泵总成', '9684544580' ,98 ,100000 union all select 3,'泵总成1','9684544580' ,97 ,100000 union all select 4,'泵总成1','9684544580' ,100,100000 union all select 5,'泵总成2','9684544580' ,50 ,100000 union all select 6,'泵总成2','9684544580' ,49 ,100000 union all select 7,'泵总成2','9684544580' ,3 ,100000 select * from @t; -- 等高手。
作者: jinfengyiye 发布时间: 2011-11-10
作者: OrchidCat 发布时间: 2011-11-10
泵总成2 9684544580 990000 5、6
这两条是怎么算的?
作者: fredrickhu 发布时间: 2011-11-10
if object_id('[TB]') is not null drop table [TB] go create table [TB] (ID int,物品 nvarchar(8),物品料号 bigint,单价 int,数量 int) insert into [TB] select 1,'泵总成',9684544580,99,100000 union all select 2,'泵总成',9684544580,98,100000 union all select 3,'泵总成1',9684544580,97,100000 union all select 4,'泵总成1',9684544580,100,100000 union all select 5,'泵总成2',9684544580,50,100000 union all select 6,'泵总成2',9684544580,49,100000 union all select 7,'泵总成2',9684544580,3,100000 select * from [TB] SELECT 物品 , 物品料号 , SUM(单价 * 数量) AS 总金额 , [ID] = CASE WHEN SUM(单价 * 数量) > 1000000 THEN STUFF(( SELECT ',' + CONVERT(VARCHAR, id) FROM tb t WHERE 物品 = tb.物品 AND 物品料号 = tb.物品料号 FOR XML PATH('') ), 1, 1, '') END FROM tb GROUP BY 物品 , 物品料号 /* 物品 物品料号 总金额 ID 泵总成 9684544580 19700000 1,2 泵总成1 9684544580 19700000 3,4 泵总成2 9684544580 10200000 5,6,7*/
作者: OrchidCat 发布时间: 2011-11-10
我觉得他是分组从上向下加,如果和超过一个数字就到下一行。下面又一直加,直到要超过了,又换一行。
感觉这种不一条一条处理很难办。
作者: jinfengyiye 发布时间: 2011-11-10
作者: roy_88 发布时间: 2011-11-10
select 物品, 物品料号 ,单价* 数量 as 金额 ,id as ID2
from @t where 单价* 数量>1000000
union all
--行转列一下id2
select 物品, 物品料号 ,单价* 数量 as 金额 ,dbo.f_行转列(..) as ID2
from @t where 单价* 数量<=1000000
...
作者: zjl8008 发布时间: 2011-11-10
第5条+第6条=990000
如果再加上第7条的话就>1000000l
作者: wangdg520 发布时间: 2011-11-10
作者: jinfengyiye 发布时间: 2011-11-10
作者: wangdg520 发布时间: 2011-11-10
/*
物品 物品料号 总金额 ID
泵总成 9684544580 19700000 1,2
泵总成1 9684544580 19700000 3,4
泵总成2 9684544580 10200000 5,6,7*/
总金额不能大于1000000的!
大于1000000的要重新汇总成一条!
作者: wangdg520 发布时间: 2011-11-10
use dbx; go declare @t table(id int,物品 nvarchar(100),物品料号 nvarchar(100),单价 int ,数量 int); insert into @t select 1,'泵总成', '9684544580' ,99 ,100000 union all select 2,'泵总成', '9684544580' ,98 ,100000 union all select 3,'泵总成1','9684544580' ,97 ,100000 union all select 4,'泵总成1','9684544580' ,100,100000 union all select 5,'泵总成2','9684544580' ,50 ,100000 union all select 6,'泵总成2','9684544580' ,49 ,100000 union all select 7,'泵总成2','9684544580' ,3 ,100000 union all select 8,'泵总成2','9684544580' ,3 ,100000 union all select 9,'泵总成2','9684544580' ,3 ,100000 union all select 10,'泵总成2','9684544580' ,3 ,100000 union all select 11,'泵总成2','9684544580' ,99 ,100000 ; --select * from @t; -- -- 这里假设id都是按物品排序id,如果没有则先排序。 create table #t(id int,物品 nvarchar(100),物品料号 nvarchar(100),单价 int ,数量 int); declare @i int ,@c int; declare @p nvarchar(100); declare @price int; select @i=0,@c=COUNT(1) from @t; while @i<=@c begin select @p=物品,@price=单价 from @t where id=@i; if exists(select 1 from #t where 物品=@p) if (select top 1 单价+@price from #t order by id desc )<=100 update #t set 单价=单价+@price where 物品=@p and id=(select MAX(id) from #t); else insert into #t select * from @t where id=@i; else insert into #t select * from @t where id=@i; set @i=@i+1; end select * from #t; --drop table #t; /* id 物品 物品料号 单价 数量 ------- ------------ ------------------------- ----------- ----------- 1 泵总成 9684544580 99 100000 2 泵总成 9684544580 98 100000 3 泵总成1 9684544580 97 100000 4 泵总成1 9684544580 100 100000 5 泵总成2 9684544580 99 100000 7 泵总成2 9684544580 12 100000 11 泵总成2 9684544580 99 100000 */ --这样效率也不高。
作者: jinfengyiye 发布时间: 2011-11-10
declare @t table(id int,物品 nvarchar(100),物品料号 nvarchar(100),单价 int ,数量 int); insert into @t select 1,'泵总成', '9684544580' ,99 ,100000 union all select 2,'泵总成', '9684544580' ,98 ,100000 union all select 3,'泵总成1','9684544580' ,97 ,100000 union all select 4,'泵总成1','9684544580' ,100,100000 union all select 5,'泵总成2','9684544580' ,50 ,100000 union all select 6,'泵总成2','9684544580' ,49 ,100000 union all select 7,'泵总成2','9684544580' ,3 ,100000 union all select 8,'泵总成2','9684544580' ,3 ,100000 union all select 9,'泵总成2','9684544580' ,3 ,100000 union all select 10,'泵总成2','9684544580' ,3 ,100000 union all select 11,'泵总成2','9684544580' ,99 ,100000 ; --select * from @t; -- -- 这里假设id都是按物品排序id,如果没有则先排序。 create table #t(id int,物品 nvarchar(100),物品料号 nvarchar(100),单价 int ,数量 int); declare @i int ,@c int; declare @p nvarchar(100); declare @price int; select @i=0,@c=COUNT(1) from @t; while @i<=@c begin select @p=物品,@price=单价 from @t where id=@i; if exists(select 1 from #t where 物品=@p) and (select top 1 单价+@price from #t order by id desc )<=100 update #t set 单价=单价+@price where 物品=@p and id=(select MAX(id) from #t); else insert into #t select * from @t where id=@i; set @i=@i+1; end select * from #t;
作者: jinfengyiye 发布时间: 2011-11-10
作者: wangdg520 发布时间: 2011-11-10
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28