报表统计
/*--原帖地址:
http://community.csdn.net/Expert/topic/3826/3826130.xml?temp=.4175836
--*/
--示例数据
--建企业名称表
CREATE TABLE qiye (qiyemingcheng char(16),xingzhi char(4),shunxu int)
insert into qiye select '企业甲','国有',1
union all select '企业乙','国有',2
union all select '企业丙','国有',3
union all select '企业丁','民营',4
union all select '企业戊','民营',5
--建指标名称表
CREATE TABLE zbmc (zhibiaoming char(20),shunxu int,flag int)
insert into zbmc select '总产值',1,1
union all select '总利润',2,1
union all select '万元产值能耗',3,-1
union all select '人均收入',4,1
union all select '万元损耗',5,-1
--建具体指标资料表企业名称、指标名称、年份、月份、当月指标数值
CREATE TABLE zhibiao (qyname char(10),zbname char(16),nian int,yue int,zhibiaozhi float)
insert into zhibiao
select '企业甲','总产值',2004,2,88888.99
union all select '企业甲','总利润',2004,2,666.66
union all select '企业甲','万元产值能耗',2004,2,2222.22
union all select '企业甲','人均收入',2004,2,2008.18
union all select '企业甲','万元损耗',2004,2,200.96
union all select '企业甲','总产值',2005,1,98888.99
union all select '企业甲','总利润',2005,1,866.66
union all select '企业甲','万元产值能耗',2005,1,2022.11
union all select '企业甲','人均收入',2005,1,2208.87
union all select '企业甲','万元损耗',2005,1,203
union all select '企业甲','总产值',2005,2,99999.99
union all select '企业甲','总利润',2005,2,888.66
union all select '企业甲','万元产值能耗',2005,2,2122.22
union all select '企业甲','人均收入',2005,2,2708.18
union all select '企业甲','万元损耗',2005,2,200.96
union all select '企业乙','总产值',2004,2,78888.99
union all select '企业乙','总利润',2004,2,666.66
union all select '企业乙','万元产值能耗',2004,2,2222.22
union all select '企业乙','人均收入',2004,2,2008.18
union all select '企业乙','万元损耗',2004,2,200.96
union all select '企业乙','总产值',2005,1,79888.99
union all select '企业乙','总利润',2005,1,876.66
union all select '企业乙','万元产值能耗',2005,1,2022.11
union all select '企业乙','人均收入',2005,1,2208.87
union all select '企业乙','万元损耗',2005,1,203
union all select '企业乙','总产值',2005,2,96999.99
union all select '企业乙','总利润',2005,2,868.66
union all select '企业乙','万元产值能耗',2005,2,2122.22
union all select '企业乙','人均收入',2005,2,2608.18
union all select '企业乙','万元损耗',2005,2,200.96
union all select '企业丙','总产值',2004,2,58888.99
union all select '企业丙','总利润',2004,2,666.66
union all select '企业丙','万元产值能耗',2004,2,2222.22
union all select '企业丙','人均收入',2004,2,2008.18
union all select '企业丙','万元损耗',2004,2,200.96
union all select '企业丙','总产值',2005,1,68888.99
union all select '企业丙','总利润',2005,1,966.66
union all select '企业丙','万元产值能耗',2005,1,2022.11
union all select '企业丙','人均收入',2005,1,2208.87
union all select '企业丙','万元损耗',2005,1,203
union all select '企业丙','总产值',2005,2,99999.99
union all select '企业丙','总利润',2005,2,888.66
union all select '企业丙','万元产值能耗',2005,2,2122.22
union all select '企业丙','人均收入',2005,2,2708.18
union all select '企业丙','万元损耗',2005,2,100.96
union all select '企业丁','总产值',2004,2,84888.99
union all select '企业丁','总利润',2004,2,646.66
union all select '企业丁','万元产值能耗',2004,2,2222.22
union all select '企业丁','人均收入',2004,2,2008.18
union all select '企业丁','万元损耗',2004,2,200.96
union all select '企业丁','总产值',2005,1,94888.99
union all select '企业丁','总利润',2005,1,856.66
union all select '企业丁','万元产值能耗',2005,1,2022.11
union all select '企业丁','人均收入',2005,1,2208.87
union all select '企业丁','万元损耗',2005,1,203
union all select '企业丁','总产值',2005,2,99999.99
union all select '企业丁','总利润',2005,2,888.66
union all select '企业丁','万元产值能耗',2005,2,2122.22
union all select '企业丁','人均收入',2005,2,2708.18
union all select '企业丁','万元损耗',2005,2,400.96
go
/*--处理要求
现在要统计全省工业企业指标情况,工业企业要分民营企业和国有企业两类,并且在统计出来时要求国有企业在民营企业的前边,要按照指标分类统计当月情况、和上月比较情况、指标提升情况、本年本月止累计、去年本月同期,指标提升情况要求当月指标减去上月指标,然后根据不同的指标情况得出是变好了还是变坏了,因为要求指标和单位都要按照事先规定的顺序显示,所以我设计了三个表,一个是企业名称表qiye,里边有企业名称、企业性质(用来区分是国企还是民营)、显示顺序(因为要求按照顺序显示和打印),一个是指标名称表zbmc,暂时里边有指标名、显示顺序,我想加个字段来表示指标提升情况的标记,比如说总产值增大后说明指标上升了,能耗升高后说明指标下降了,但我现在还没想出怎样定义这个字段,所以暂时用了个标记,它的值是1时表示 指标增到后说明指标是提升了,-1时表示该指标值增大后说明该指标下降了,最后一个表是具体指标资料表zhibiao,里边有企业名称、指标名称、年份、月份、当月指标数值
请问怎样用怎样的SQL语句才能得出如下格式的结果:假设是统计今年2月份数据
指标名称 单位名称 指标值 和上月比 指标提升情况 本年1-2月累计 去年同期值
-----------------------------------------
总产值 全省
国营
民营
企业甲
企业乙
企业丙
企业丁
总利润 全省
国营
民营
企业甲
企业乙
企业丙
企业丁
万元产值能耗 全省
国营
民营
企业甲
企业乙
企业丙
企业丁
--*/
--查询的存储过程
create proc p_qry
@year int, --要查询的年份
@month int --要查询的月份
as
set nocount on
declare @y int,@m int,@y1 int
select @y1=@year-1
if @month=1
select @y=@year-1,@m=12
else
select @y=@year,@m=@month-1
select case when a.s2=0 then a.zhibiaoming else '' end as 指标名称
,a.qiyemingcheng as 单位名称
,指标值=cast(a.指标值 as decimal(10,2))
,上月指标值=cast(isnull(b.上期,0) as decimal(10,2))
,和上月比=case
when isnull(b.上期,0)=0 then '0.00%'
else cast(cast(a.指标值*100/b.上期 as decimal(10,2)) as varchar)+'%' end
,指标提升情况=substring('↓-↑',cast(sign(a.指标值-isnull(b.上期,0))*a.flag as int)+2,1)
+cast(abs(cast(a.指标值-isnull(b.上期,0) as decimal(10,2))) as varchar)
,本年累计=cast(a.本年累计 as decimal(10,2))
,去年同期值=cast(isnull(c.同期,0) as decimal(10,2))
from(
--本期
select zb.zhibiaoming
,qiyemingcheng=case
when grouping(qy.xingzhi)=1 then N'全省'
when grouping(qy.qiyemingcheng)=1 then qy.xingzhi
else qy.qiyemingcheng end
,指标值=sum(case when c.yue=@month then zhibiaozhi end)
,本年累计=sum(zhibiaozhi)
,flag=max(zb.flag)
,s1=min(zb.shunxu),s2=1-grouping(qy.xingzhi)
,s3=1-grouping(qy.qiyemingcheng),s4=min(qy.shunxu)
from zbmc zb,qiye qy,zhibiao c
where c.nian=@year and c.yue<=@month
and c.qyname=qy.qiyemingcheng
and c.zbname=zb.zhibiaoming
group by zb.zhibiaoming
,qy.xingzhi,qy.qiyemingcheng
with rollup
having grouping(zb.zhibiaoming)=0
)a left join(
--上期
select zb.zhibiaoming
,qiyemingcheng=case
when grouping(qy.xingzhi)=1 then N'全省'
when grouping(qy.qiyemingcheng)=1 then qy.xingzhi
else qy.qiyemingcheng end
,上期=sum(zhibiaozhi)
,s1=min(zb.shunxu),s2=1-grouping(qy.xingzhi)
,s3=1-grouping(qy.qiyemingcheng),s4=min(qy.shunxu)
from zbmc zb,qiye qy,zhibiao c
where c.nian=@y and c.yue=@m
and c.qyname=qy.qiyemingcheng
and c.zbname=zb.zhibiaoming
group by zb.zhibiaoming
,qy.xingzhi,qy.qiyemingcheng
with rollup
having grouping(zb.zhibiaoming)=0
)b on a.zhibiaoming=b.zhibiaoming and a.qiyemingcheng=b.qiyemingcheng
left join(
--同期
select zb.zhibiaoming
,qiyemingcheng=case
when grouping(qy.xingzhi)=1 then N'全省'
when grouping(qy.qiyemingcheng)=1 then qy.xingzhi
else qy.qiyemingcheng end
,同期=sum(zhibiaozhi)
,s1=min(zb.shunxu),s2=1-grouping(qy.xingzhi)
,s3=1-grouping(qy.qiyemingcheng),s4=min(qy.shunxu)
from zbmc zb,qiye qy,zhibiao c
where c.nian=@y1 and c.yue=@month
and c.qyname=qy.qiyemingcheng
and c.zbname=zb.zhibiaoming
group by zb.zhibiaoming
,qy.xingzhi,qy.qiyemingcheng
with rollup
having grouping(zb.zhibiaoming)=0
)c on a.zhibiaoming=c.zhibiaoming and a.qiyemingcheng=c.qiyemingcheng
order by a.s1,a.s2,a.s3,a.s4
go
--调用
exec p_qry 2005,2
go
--删除测试
drop table qiye,zbmc,zhibiao
drop proc p_qry
/*测试结果自己看*/