现在已知一个表里的数据为
年月 产值 苏南产值 苏北产值 苏中产值
200806 897897897 432543252 432454342 53524432
200807 997897897 322543252 432454342 53524432
200808 1097897897 3443253543 232434324 43434324
能不能通过一个存储过程得到下面的内容
当输入一个年月以后
当月产值 上月产值 当月/上月 上上月 上月/上上月
全部
苏南
苏北
苏中
年月 产值 苏南产值 苏北产值 苏中产值
200806 897897897 432543252 432454342 53524432
200807 997897897 322543252 432454342 53524432
200808 1097897897 3443253543 232434324 43434324
能不能通过一个存储过程得到下面的内容
当输入一个年月以后
当月产值 上月产值 当月/上月 上上月 上月/上上月
全部
苏南
苏北
苏中
select 地区,当月产值,上月产值,[当月/上月]=当月产值*1.0/上月产值,上上月,[上月/上上月]=上月产值*1.0/上上月 from (
select 地区='全部',
当月产值=case when month(年月)=month(getdate()) then 苏南产值+苏北产值+苏中产值 else 0 end,
上月产值=case when month(年月)=month(getdate()-1) then 苏南产值+苏北产值+苏中产值 else 0 end,
上上月=case when month(年月)=month(getdate()-2) then 苏南产值+苏北产值+苏中产值 else 0 end
from tb union all select 地区='苏南',
当月产值=case when month(年月)=month(getdate()) then 苏南产值 else 0 end,
上月产值=case when month(年月)=month(getdate()-1) then 苏南产值 else 0 end,
上上月=case when month(年月)=month(getdate()-2) then 苏南产值 else 0 end
from tb union all select 地区='苏北',
当月产值=case when month(年月)=month(getdate()) then 苏北产值 else 0 end,
上月产值=case when month(年月)=month(getdate()-1) then 苏北产值 else 0 end,
上上月=case when month(年月)=month(getdate()-2) then 苏北产值 else 0 end
from tb union all select 地区='苏中',
当月产值=case when month(年月)=month(getdate()) then 苏中产值 else 0 end,
上月产值=case when month(年月)=month(getdate()-1) then 苏中产值 else 0 end,
上上月=case when month(年月)=month(getdate()-2) then 苏中产值 else 0 end
from tb
)t
create proc pp
@year_month varchar(6)
as select 地区,当月产值,上月产值,[当月/上月]=当月产值*1.0/上月产值,上上月,[上月/上上月]=上月产值*1.0/上上月 from (
select 地区='全部',
当月产值=case when datediff(month,年月,getdate())=0 then 苏南产值+苏北产值+苏中产值 else 0 end,
上月产值=case when datediff(month,年月,getdate())=1 then 苏南产值+苏北产值+苏中产值 else 0 end,
上上月=case when datediff(month,年月,getdate())=2 then 苏南产值+苏北产值+苏中产值 else 0 end
from tb union all select 地区='苏南',
当月产值=case when datediff(month,年月,getdate())=0 then 苏南产值 else 0 end,
上月产值=case when datediff(month,年月,getdate())=1 then 苏南产值 else 0 end,
上上月=case when datediff(month,年月,getdate())=2 then 苏南产值 else 0 end
from tb union all select 地区='苏北',
当月产值=case when datediff(month,年月,getdate())=0 then 苏北产值 else 0 end,
上月产值=case when datediff(month,年月,getdate())=1 then 苏北产值 else 0 end,
上上月=case when datediff(month,年月,getdate())=2 then 苏北产值 else 0 end
from tb union all select 地区='苏中',
当月产值=case when datediff(month,年月,getdate())=0 then 苏中产值 else 0 end,
上月产值=case when datediff(month,年月,getdate())=1 then 苏中产值 else 0 end,
上上月=case when datediff(month,年月,getdate())=2 then 苏中产值 else 0 end
from tb
)t
goexec pp '200807'
CREATE TABLE test3(ny CHAR(10), cz DECIMAL(11,0), sncz DECIMAL(11,0), sbcz DECIMAL(11,0), szcz DECIMAL(11,0));INSERT INTO test3(ny, cz, sncz, sbcz, szcz)
SELECT '200806',897897897,432543252,432454342,53524432 UNION ALL
SELECT '200807',997897897,322543252,432454342,53524432 UNION ALL
SELECT '200808',1097897897,3443253543,232434324,43434324;/*
exec test_proc3 '200808'
*/
--年月变量 暂时用数据,这样会出问题:
--比如你输入 参数 '200801'的话,是得不到07年的12月和11月的数据的
--有待改善
ALTER PROCEDURE test_proc3 @ny CHAR(10)AS
BEGIN
CREATE TABLE #DBTemp(
Id INT IDENTITY(1,1),
nameds CHAR(10),
dycz DECIMAL(11,0),
sycz DECIMAL(11,0),
dsycz DECIMAL(11,0),
ssycz DECIMAL(11,0),
sssycz DECIMAL(11,0) )
INSERT INTO #DBTemp(nameds,dycz, sycz, ssycz)
SELECT '全部',SUM(CASE ny WHEN @ny THEN cz ELSE 0 END),SUM(CASE ny WHEN @ny-1 THEN cz ELSE 0 END),SUM(CASE ny WHEN @ny-2 THEN cz ELSE 0 END) FROM test3 UNION ALL
SELECT '苏南',SUM(CASE ny WHEN @ny THEN sncz ELSE 0 END),SUM(CASE ny WHEN @ny-1 THEN sncz ELSE 0 END),SUM(CASE ny WHEN @ny-2 THEN sncz ELSE 0 END) FROM test3 UNION ALL
SELECT '苏北',SUM(CASE ny WHEN @ny THEN sbcz ELSE 0 END),SUM(CASE ny WHEN @ny-1 THEN sbcz ELSE 0 END),SUM(CASE ny WHEN @ny-2 THEN sbcz ELSE 0 END) FROM test3 UNION ALL
SELECT '苏中',SUM(CASE ny WHEN @ny THEN szcz ELSE 0 END),SUM(CASE ny WHEN @ny-1 THEN szcz ELSE 0 END),SUM(CASE ny WHEN @ny-2 THEN szcz ELSE 0 END) FROM test3 ; UPDATE #DBTemp SET dsycz = dycz + sycz;
UPDATE #DBTemp SET sssycz = sycz + ssycz; SELECT SUBSTRING(@ny,1,4)+'年'+SUBSTRING(@ny,5,2)+'月' AS '明细','当月产值' '当月产值', '上月产值' '上月产值', '当月/上月' '当月/上月', '上上月' '上上月', '上月/上上月' '上月/上上月' UNION ALL
SELECT nameds, CONVERT(VARCHAR(14),dycz), CONVERT(VARCHAR(14),sycz), CONVERT(VARCHAR(14),dsycz), CONVERT(VARCHAR(14),ssycz), CONVERT(VARCHAR(14),sssycz) FROM #DBTEMP;
DROP TABLE #DBTemp;END