22,210
社区成员
发帖
与我相关
我的任务
分享
Create table test
(ids bigint identity(1,1) primary key,
names nvarchar(20),--名字
classify nvarchar(10),--分类
years int,--年
months int,--月
num bigint--数量
)
insert into test select 'phone1','A','2009',1,100 union all
select 'phone1','A','2009',2,200 union all
select 'phone1','A','2009',3,300 union all
select 'phone1','A','2009',4,400 union all
select 'phone1','A','2009',5,500 union all
select 'phone1','A','2009',6,600 union all
select 'phone1','A','2009',7,700 union all
select 'phone1','A','2009',8,800 union all
select 'phone1','A','2009',9,900 union all
select 'phone1','A','2009',10,1000 union all
select 'phone1','A','2009',11,1100 union all
select 'phone1','A','2009',12,1200 union all
select 'phone1','A','2010',1,100 union all
select 'phone1','A','2010',2,200 union all
select 'phone1','A','2010',3,300 union all
select 'phone1','A','2010',4,400 union all
select 'phone1','A','2010',5,500 union all
select 'phone1','A','2010',6,600 union all
select 'phone1','A','2010',7,700 union all
select 'phone1','A','2010',8,800 union all
select 'phone1','A','2010',9,900 union all
select 'phone1','A','2010',10,1000 union all
select 'phone1','A','2010',11,1100 union all
select 'phone1','A','2010',12,1200 union all
select 'phone1','B','2009',1,100 union all
select 'phone1','B','2009',2,200 union all
select 'phone1','B','2009',3,300 union all
select 'phone1','B','2009',4,400 union all
select 'phone1','B','2009',5,500 union all
select 'phone1','B','2009',6,600 union all
select 'phone1','B','2009',7,700 union all
select 'phone1','B','2009',8,800 union all
select 'phone1','B','2009',9,900 union all
select 'phone1','B','2009',10,1000 union all
select 'phone1','B','2009',11,1100 union all
select 'phone1','B','2009',12,1200 union all
select 'phone1','B','2010',1,100 union all
select 'phone1','B','2010',2,200 union all
select 'phone1','B','2010',3,300 union all
select 'phone1','B','2010',4,400 union all
select 'phone1','B','2010',5,500 union all
select 'phone1','B','2010',6,600 union all
select 'phone1','B','2010',7,700 union all
select 'phone1','B','2010',8,800 union all
select 'phone1','B','2010',9,900 union all
select 'phone1','B','2010',10,1000 union all
select 'phone1','B','2010',11,1100 union all
select 'phone1','B','2010',12,1200
go
declare @year as int
declare @month as int
set @year = 2010
set @month = 5
select 名字 = names,
分类 = classify ,
数量 = m.num ,
当月同比 = cast(m.num * 1.0 / (select num from test n where n.names = m.names and n.classify = m.classify and n.years = @year - 1 and n.months = @month) as decimal(18,2)),
当月环比 = cast((m.num - (select top 1 num from test n where n.names = m.names and n.classify = m.classify and ltrim(n.years) + right('0'+ltrim(n.months),2) < ltrim(m.years) + right('0'+ltrim(m.months),2) order by n.years desc , n.months desc))*1.0/
(select top 1 num from test n where n.names = m.names and n.classify = m.classify and ltrim(n.years) + right('0'+ltrim(n.months),2) < ltrim(m.years) + right('0'+ltrim(m.months),2) order by n.years desc , n.months desc) as decimal(18,2)),
累计数量 = (select sum(num) from test n where n.names = m.names and n.classify = m.classify and n.years = m.years and n.months <= m.months),
累计当月同比 = cast((select sum(num) from test n where n.names = m.names and n.classify = m.classify and n.years = m.years and n.months <= m.months) * 1.0 /
(select sum(num) from test n where n.names = m.names and n.classify = m.classify and n.years = m.years - 1 and n.months <= m.months) as decimal(18,2)),
累计当月环比 = cast(((select sum(num) from test n where n.names = m.names and n.classify = m.classify and n.years = m.years and n.months <= m.months) - (select sum(num) from test n where n.names = m.names and n.classify = m.classify and n.years = m.years - 1 and n.months <= m.months))*1.0/
(select sum(num) from test n where n.names = m.names and n.classify = m.classify and n.years = m.years - 1 and n.months <= m.months) as decimal(18,2))
from test m
where m.years = @year and m.months = @month
drop table test
/*
名字 分类 数量 当月同比 当月环比 累计数量 累计当月同比 累计当月环比
-------------------- ---------- -------------------- -------------------- -------------------- -------------------- -------------------- --------------------
phone1 A 500 1.00 .25 1500 1.00 .00
phone1 B 500 1.00 .25 1500 1.00 .00
(所影响的行数为 2 行)
*/