有下面数据
a b c
271108 1.00 0.01
271109 1.00 0.01
271110 1.00 0.01
271111 1.00 0.01
271112 1.00 1.00
271113 2.00 0.01
271114 1.00 0.01
271115 1.00 0.01 得到的结果
a sum(b) sum(c)
271108 4.00 0.04
271112 1.00 1.00
271113 2.00 0.01
271114 2.00 0.02 也就是分别比较 b值和c,如果连续的b c值相同则求和,并得到第一个a值
不要游标方式,谢谢
a b c
271108 1.00 0.01
271109 1.00 0.01
271110 1.00 0.01
271111 1.00 0.01
271112 1.00 1.00
271113 2.00 0.01
271114 1.00 0.01
271115 1.00 0.01 得到的结果
a sum(b) sum(c)
271108 4.00 0.04
271112 1.00 1.00
271113 2.00 0.01
271114 2.00 0.02 也就是分别比较 b值和c,如果连续的b c值相同则求和,并得到第一个a值
不要游标方式,谢谢
-- Author : htl258(Tony)
-- Date : 2010-04-21 20:56:37
-- Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
--------------------------------------------------------------------------
--> 生成测试数据表:tbIF NOT OBJECT_ID('[tb]') IS NULL
DROP TABLE [tb]
GO
CREATE TABLE [tb]([a] INT,[b] DECIMAL(18,2),[c] decimal(9,2))
INSERT [tb]
SELECT 271108,1.00,'0.01' UNION ALL
SELECT 271109,1.00,'0.01' UNION ALL
SELECT 271110,1.00,'0.01' UNION ALL
SELECT 271111,1.00,'0.01' UNION ALL
SELECT 271112,1.00,'1.00' UNION ALL
SELECT 271113,2.00,'0.01' UNION ALL
SELECT 271114,1.00,'0.01' UNION ALL
SELECT 271115,1.00,'0.01'
GO
--SELECT * FROM [tb]-->SQL查询如下:
alter table tb add d int
go
declare @j int,@i varchar(20)
update tb set d = @j, @j = case when @i = right(1000+b,4)+right(1000+c,4) then @j else isnull(@j,0)+1 end,@i = right(1000+b,4)+right(1000+c,4)
select min(a) a,sum(b) b,sum(c) c from tb group by b,c,d order by a
/*
a b c
----------- --------------------------------------- ---------------------------------------
271108 4.00 0.04
271112 1.00 1.00
271113 2.00 0.01
271114 2.00 0.02(4 行受影响)*/
go
declare @Tab table
(
a int, b decimal(10,2), c decimal(10,2))
insert into @Tab
select '271108', 1.00, 0.01 union all
select '271109', 1.00, 0.01 union all
select '271110', 1.00, 0.01 union all
select '271111', 1.00, 0.01 union all
select '271112', 1.00, 1.00 union all
select '271113', 2.00, 0.01 union all
select '271114', 1.00, 0.01 union all
select '271115', 1.00, 0.01
select * from (
select a,b,c,ROW_NUMBER() over(partition by b,c order by a) rownumber
from (
select a,SUM(b) over(partition by (a-rownumber),b,c) b,SUM(c) over(partition by (a-rownumber),b,c) c
from (
select a,B,c,ROW_NUMBER() over(partition by a-rownumber,b,c order by a,b,c) rownumber
from (
select a,b,c,row_number() over(order by a,b,c) rownumber from @Tab)T
)T) T
) T where rownumber = 1a b c rownumber
----------- --------------------------------------- --------------------------------------- --------------------
271112 1.00 1.00 1
271113 2.00 0.01 1
271114 2.00 0.02 1
271108 4.00 0.04 1(4 行受影响)
-- Author :SQL77(只为思齐老)
-- Date :2010-04-21 21:01:55
-- Version:
-- Microsoft SQL Server 2005 - 9.00.1399.06 (Intel X86)
-- Oct 14 2005 00:33:37
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:#tb
if object_id('tempdb.dbo.#tb') is not null drop table #tb
go
create table #tb([a] int,[b] numeric(3,2),[c] numeric(3,2))
insert #tb
select 271108,1.00,0.01 union all
select 271109,1.00,0.01 union all
select 271110,1.00,0.01 union all
select 271111,1.00,0.01 union all
select 271112,1.00,1.00 union all
select 271113,2.00,0.01 union all
select 271114,1.00,0.01 union all
select 271115,1.00,0.01
--------------开始查询--------------------------
select *,0 num into #t from #tb
declare @num int, @b numeric(3,2),@c numeric(3,2)
set @num=0
update #t set num=@num,
@num=case when b=@b and c=@c then @num else @num+1 end,
@b=b,@c=cselect min(a),
sum(B)b,sum(c)c
from #t t
group by num
----------------结果----------------------------
/*
b c
----------- --------------------------------------- ---------------------------------------
271108 4.00 0.04
271112 1.00 1.00
271113 2.00 0.01
271114 2.00 0.02(4 行受影响)
*/
go-->生成测试数据
declare @Tab table
(
a int, b decimal(10,2), c decimal(10,2))
insert into @Tab
select '271108', 1.00, 0.01 union all
select '271109', 1.00, 0.01 union all
select '271110', 1.00, 0.01 union all
select '271111', 1.00, 0.01 union all
select '271112', 1.00, 1.00 union all
select '271113', 2.00, 0.01 union all
select '271114', 1.00, 0.01 union all
select '271115', 1.00, 0.01 -->测试SQL
select MIN (a) a,SUM(B) sumb,SUM(c) sumc
from (
select a,b,c,a - (select COUNT(1)
from @Tab where a < a.a and b = a.b and c = a.c) aid from @Tab a
) T
group by aid-->测试结果
/*
a sumb sumc
----------- ---------------------------------------
271108 4.00 0.04
271114 2.00 0.02
271112 1.00 1.00
271113 2.00 0.01
*/
go-->生成测试数据
declare @Tab table
(
a int, b decimal(10,2), c decimal(10,2))
insert into @Tab
select '271108', 1.00, 0.01 union all
select '271109', 1.00, 0.01 union all
select '271110', 1.00, 0.01 union all
select '271111', 1.00, 0.01 union all
select '271112', 1.00, 1.00 union all
select '271113', 2.00, 0.01 union all
select '271114', 1.00, 0.01 union all
select '271115', 1.00, 0.01 -->测试SQL
select MIN (a) a,SUM(B) sumb,SUM(c) sumc
from (
select a,b,c,a - (select COUNT(1)
from @Tab where a < a.a and b = a.b and c = a.c) aid from @Tab a
) T
group by aid
order by aid-->测试结果
a sumb sumc
----------- --------------------------------------- ---------------------------------------
271108 4.00 0.04
271114 2.00 0.02
271112 1.00 1.00
271113 2.00 0.01(4 行受影响)