+ -
当前位置:首页 → 问答吧 → 不要用循环实现行汇总

不要用循环实现行汇总

时间:2011-11-10

来源:互联网

根据 物品、物品料号、单价 进行汇总,金额大于1000000就在另一行。
同时记录该笔汇总的明细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

SQL code

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

没看明白,为啥5,6 要加在一起,而1,2/3,4 不再一起

作者: OrchidCat   发布时间: 2011-11-10

泵总成1 9684544580 1000000 4
泵总成2 9684544580 990000 5、6


这两条是怎么算的?

作者: fredrickhu   发布时间: 2011-11-10

SQL code
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

用CTE處理吧

作者: roy_88   发布时间: 2011-11-10

看明白了,金额大于1000000的不汇总
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

4楼兄弟的结果 :
/*
物品 物品料号 总金额 ID
泵总成 9684544580 19700000 1,2
泵总成1 9684544580 19700000 3,4
泵总成2 9684544580 10200000 5,6,7*/


总金额不能大于1000000的!
大于1000000的要重新汇总成一条!

作者: wangdg520   发布时间: 2011-11-10

SQL code

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

SQL code

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

没有找到好办法。单层循环的我自己能做!跟jinfengyiye的差不多的!所有不能给分了。抱歉

作者: wangdg520   发布时间: 2011-11-10