34,590
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据:@test
declare @test table([type] varchar(1),[bill] int,[free] int)
insert @test
select 'A',1,50 union all
select 'A',2,100 union all
select 'A',3,150 union all
select 'A',4,200 union all
select 'A',9,500 union all
select 'B',12,200 union all
select 'C',1,50 union all
select 'C',3,150 union all
select 'C',4,200
;with b as
(select * from @test t where not exists(select 1 from @test where [type]=t.[type] and [bill]=t.[bill]+1))
select a.[type], start=min(a.[bill]), [end]=max(a.[bill]), [sum(free)]=sum(a.[free])
from @test a join b on a.[type]=b.[type] -- and a.[bill]<=b.[bill]
where b.[bill] = (select min([bill]) from b t where [type]=b.[type] and [bill]>=a.[bill])
group by a.[type], b.[bill]
order by a.[type], start
/*
type start end sum(free)
---- ----------- ----------- -----------
A 1 4 500
A 9 9 500
B 12 12 200
C 1 1 50
C 3 4 350
(5 行受影响)
*/
select
a.type,
min(a.bill) as min_bill ,
c.max_bill,
sum(free) as sum_free
from
(select
type,
bill,
(select min(bill)
from Ta b
where b.type=a.type and b.bill>=a.bill
and not exists(select 1
from Ta
where type=b.type and bill=b.bill+1)) as max_bill
from Ta a ) c
left join ta a on c.type = a.type and a.bill = c.bill
group by a.type,c.max_bill
order by type
declare @test table (
type varchar(2),
bill int,
free int
)
insert @test select
'A', 1 , 50
union all select
'A', 2 , 100
union all select
'A', 3 , 150
union all select
'A', 4 , 200
union all select
'A', 9 , 500
union all select
'B', 12, 200
union all select
'C', 1 , 50
union all select
'C', 3 , 150
union all select
'C', 4 , 200
select a.type,a.bill as start,b.bill as [end],sum(c.free) as [sum(free)]
from (
select * from @test t where not exists (select 1 from @test where type=t.type and bill=t.Bill-1)
) as a,(
select * from @test t where not exists (select 1 from @test where type=t.type and bill=t.Bill+1)
) as b,@Test c
where a.type=b.type
and a.bill<=b.bill
and not exists (
select 1 from @test t where not exists (select 1 from @test where type=t.type and bill=t.Bill+1)
and type=b.type and bill<b.bill and bill>=a.bill
)
and c.type=a.type
and c.bill between a.bill and b.bill
group by a.type,a.bill,b.bill
--结果
type start end sum(free)
---- ----------- ----------- -----------
A 1 4 500
A 9 9 500
B 12 12 200
C 1 1 50
C 3 4 350
(所影响的行数为 5 行)
--就直接點寫了,可以改改
create table T(type char(01), bill int, [free] int)
insert into T select 'A',1,50
insert into T select 'A',2,100
insert into T select 'A',3,150
insert into T select 'A',4,200
insert into T select 'A',6,88888
insert into T select 'A',9,500
insert into T select 'B',12,200
insert into T select 'C',1,50
insert into T select 'C',3,150
insert into T select 'C',4,200
GO
select type, min_bill as [start], max_bill as [end],
[sum(free)]=(select sum([free]) from T where type=X.type and bill>=min_bill and bill <=max_bill)
from
(
select type, bill as min_bill,(select min(bill) from T b where b.type=a.type and b.bill>=a.bill
and not exists(select 1 from T where type=b.type and bill=b.bill+1)) as max_bill
from T a
where not exists(select 1 from T where type=a.type and bill=a.bill-1)
) X
/* --result:
type start end sum(free)
---- ----------- ----------- -----------
A 1 4 500
A 6 6 88888
A 9 9 500
B 12 12 200
C 1 1 50
C 3 4 350
*/
GO
drop table T