62,268
社区成员
发帖
与我相关
我的任务
分享
declare @table table([id] int,[pId] varchar(20),[name] varchar(20),[money] float,[date] datetime)
insert into @table select 1,'a123','哈哈企业',50,'2007-11-04'
union all select 2,'a123','哈哈企业',100,'2008-11-04'
union all select 3,'a123','哈哈企业',300,'2008-11-04'
union all select 4,'a123','哈哈企业',200,'2009-11-04'
union all select 5,'b321','呵呵企业',100,'2007-11-04'
union all select 6,'b321','呵呵企业',50,'2008-11-04'
union all select 7,'b321','呵呵企业',30,'2008-11-04'
union all select 8,'b321','呵呵企业',20,'2009-11-04'
select * from @table
select min(a.[id]) [id],a.[pId],a.[name],
(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2007-11-04' and '2008-11-04' ) sum1,
(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2008-11-04' and '2009-11-04' ) sum2,
(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2008-11-04' and '2009-11-04' )
-(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2007-11-04' and '2008-11-04') increase,
cast(cast(
((select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2008-11-04' and '2009-11-04' )
-(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2007-11-04' and '2008-11-04'))
/(select sum([money]) from @table t where t.[pId]=a.[pId] and t.[date] between '2008-11-04' and '2009-11-04' )*100 as int) as varchar(20))+'%' increase1
from @table a group by a.[pId],a.[name]
----先看看这个有没报错
select id,企业编号,case when 时间 in(2007-11-04 ,2008-11-04 ) then sum(金额) else 0 as 时间段1的金额汇总,
case when 时间 in(2008-11-04 ,2009-11-04 ) then sum(金额) else 0 as 时间段2的金额汇总,from tab order by 企业编号