27,582
社区成员




declare @month nvarchar(10),@s nvarchar(4000)
select top 1 @month=month([riqi]),@s='' from #T order by month([riqi]) desc
while @month>0
select @s=@s+',['+@month+N'金额]=sum(case when month([riqi])='+@month+' then Price else 0 end)',@month=@month-1
exec('select [kehu]'+@s+
'from
(select a.[kehu],case when b.[yue]<a.sumPrice-(a.[danjia]*a.[shuliang]) then 0 when b.[yue]<a.sumPrice then b.[yue]-(a.sumPrice-(a.[danjia]*a.[shuliang]))
else a.[danjia]*a.[shuliang] end as Price,[riqi]
from
(select *,sumPrice=(select sum([danjia]*[shuliang]) from #T where [kehu]=a.[kehu] and [riqi]>=a.[riqi])from #T a)a
join
#T2 b on a.[kehu]=b.[kehu])T
group by [kehu],year([riqi])')
/*
kehu 8金额 7金额 6金额 5金额 4金额 3金额 2金额 1金额
---- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
A 800.00 400.00 0.00 0.00 0.00 0.00 0.00 0.00
B 300.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
C 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
D 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
(4 個資料列受到影響)
*/
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([kehu] nvarchar(1),[danjia] decimal(18,2),[shuliang] int,[riqi] Datetime)
Insert #T
select N'A',10.00,50,'2008-8-8' union all
select N'A',10.00,30,'2008-8-1' union all
select N'A',10.00,40,'2008-7-25' union all
select N'A',20.00,25,'2008-6-3' union all
select N'B',15.00,40,'2008-8-2' union all
select N'B',10.00,60,'2008-7-5' union all
select N'C',15.00,40,'2008-6-6' union all
select N'D',20.00,30,'2008-2-28'
Go
if not object_id('Tempdb..#T2') is null
drop table #T2
Go
Create table #T2([kehu] nvarchar(1),[yue] decimal(18,2))
Insert #T2
select N'A',1200.00 union all
select N'B',300.00 union all
select N'C',0 union all
select N'D',0
Go
declare @month nvarchar(10),@s nvarchar(4000)
select top 1 @month=month([riqi]),@s='' from #T order by month([riqi]) desc
while @month>0
select @s=@s+',['+@month+N'金额]=sum(case when month([riqi])='+@month+' then Price else 0 end)',@month=@month-1
exec('select [kehu]'+@s+
'from
(select a.[kehu],case when b.[yue]>=a.sumPrice then 0 when b.[yue]>a.sumPrice-(a.[danjia]*a.[shuliang]) then a.sumPrice-b.[yue] --改改
else a.[danjia]*a.[shuliang] end as Price,[riqi]
from
(select *,sumPrice=(select sum([danjia]*[shuliang]) from #T where [kehu]=a.[kehu] and [riqi]<=a.[riqi])from #T a)a
join
#T2 b on a.[kehu]=b.[kehu])T
group by [kehu],year([riqi])')
kehu 8金额 7金额 6金额 5金额 4金额 3金额 2金额 1金额
---- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
A 500.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
B 600.00 300.00 0.00 0.00 0.00 0.00 0.00 0.00
C 0.00 0.00 600.00 0.00 0.00 0.00 0.00 0.00
D 0.00 0.00 0.00 0.00 0.00 0.00 600.00 0.00
(4 個資料列受到影響)
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([kehu] nvarchar(1),[danjia] decimal(18,2),[shuliang] int,[riqi] Datetime)
Insert #T
select N'A',10.00,50,'2008-8-8' union all
select N'A',10.00,30,'2008-8-1' union all
select N'A',10.00,40,'2008-7-25' union all
select N'A',20.00,25,'2008-6-3' union all
select N'B',15.00,40,'2008-8-2' union all
select N'B',10.00,60,'2008-7-5' union all
select N'C',15.00,40,'2008-6-6' union all
select N'D',20.00,30,'2008-2-28'
Go
if not object_id('Tempdb..#T2') is null
drop table #T2
Go
Create table #T2([kehu] nvarchar(1),[yue] decimal(18,2))
Insert #T2
select N'A',1200.00 union all
select N'B',300.00 union all
select N'C',0 union all
select N'D',0
Go
declare @month nvarchar(10),@s nvarchar(4000)
select top 1 @month=month([riqi]),@s='' from #T order by month([riqi]) desc
while @month>0
select @s=@s+',['+@month+N'金额]=sum(case when month([riqi])='+@month+' then Price else 0 end)',@month=@month-1
exec('select [kehu]'+@s+
'from
(select a.[kehu],case when b.[yue]>=a.sumPrice then 0 when b.[yue]>a.sumPrice-(a.[danjia]*a.[shuliang]) then a.sumPrice-(a.[danjia]*a.[shuliang])
else a.[danjia]*a.[shuliang] end as Price,[riqi]
from
(select *,sumPrice=(select sum([danjia]*[shuliang]) from #T where [kehu]=a.[kehu] and [riqi]<=a.[riqi])from #T a)a
join
#T2 b on a.[kehu]=b.[kehu])T
group by [kehu],year([riqi])')
kehu 8金额 7金额 6金额 5金额 4金额 3金额 2金额 1金额
---- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
A 500.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
B 600.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
C 0.00 0.00 600.00 0.00 0.00 0.00 0.00 0.00
D 0.00 0.00 0.00 0.00 0.00 0.00 600.00 0.00
(4 個資料列受到影響)
if not object_id('Tempdb..#T') is null
drop table #T
Go
Create table #T([kehu] nvarchar(1),[danjia] decimal(18,2),[shuliang] int,[riqi] Datetime)
Insert #T
select N'A',10.00,50,'2008-8-8' union all
select N'A',10.00,30,'2008-8-1' union all
select N'A',10.00,40,'2008-7-25' union all
select N'A',20.00,25,'2008-6-3' union all
select N'B',15.00,40,'2008-8-2' union all
select N'B',10.00,60,'2008-7-5' union all
select N'C',15.00,40,'2008-6-6' union all
select N'D',20.00,30,'2008-2-28'
Go
declare @month nvarchar(10),@s nvarchar(4000)
select top 1 @month=month([riqi]),@s='' from #T order by month([riqi]) desc
while @month>0
select @s=@s+',['+@month+N'金额]=sum(case when month([riqi])='+@month+' then [danjia]*[shuliang] else 0 end)',@month=@month-1
exec('select [kehu]'+@s+' from #T group by [kehu],year([riqi])')
kehu 8金额 7金额 6金额 5金额 4金额 3金额 2金额 1金额
---- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- ---------------------------------------
A 800.00 400.00 500.00 0.00 0.00 0.00 0.00 0.00
B 600.00 600.00 0.00 0.00 0.00 0.00 0.00 0.00
C 0.00 0.00 600.00 0.00 0.00 0.00 0.00 0.00
D 0.00 0.00 0.00 0.00 0.00 0.00 600.00 0.00
(4 個資料列受到影響)