IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetMonitorData]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetMonitorData]
GO

--============================================================
-- Author:		hanfei
-- Date:		2011-11-27
-- Description:	获取监测数据，用于数据上报、监测情况查询和统计
--============================================================
CREATE procedure [dbo].[GetMonitorData]
	@enterType int,				--企业类型 1:污染源,2:污水处理厂,3:固废处理厂
	@controlType int,			--国控类型 1:废水国控,2:废气国控
	@enterTable nvarchar(50),	--企业数据源表
	@pollutionCode nvarchar(max),--监测项目编号
	@outTable nvarchar(50)		--输出数据表名称
as
declare @pollutionWhere nvarchar(max)	--监测项目查询条件
set @pollutionWhere = ''
if(@pollutionCode<>'')
	begin 
		set @pollutionWhere = @pollutionWhere + ' and 监测项目编号 in ('+@pollutionCode+')'
	end
--设置需要关联的排口表
declare @sqlPoint nvarchar(max)
if(@enterType=1 and @controlType=1)
	begin
		set @sqlPoint = 'select 监测年,企业代码,监测点名称,监测点代码,企业类型 from dbo.业务_废水排口信息'
	end 
if(@enterType=1 and @controlType=2)
	begin
		set @sqlPoint = 'select 监测年,企业代码,监测点名称,监测点代码,企业类型 from dbo.业务_废气监测点信息'
	end
if(@enterType=3)
	begin
		set @sqlPoint = 'select 监测年,企业代码,监测点名称,监测点代码,企业类型 from dbo.业务_废水排口信息
						 union all
						 select 监测年,企业代码,监测点名称,监测点代码,企业类型 from dbo.业务_废气监测点信息'
	end
--设置需要关联的监测数据表
declare @sqlData nvarchar(max)
if(@enterType=1 and @controlType=1)
	begin
		set @sqlData = 'select distinct 企业代码,监测点代码,监测点名称,监测项目编号,监测年,季度,企业类型 from dbo.业务_废水监测数据
						 where 污染物浓度 is not null'+@pollutionWhere
	end 
if(@enterType=1 and @controlType=2)
	begin
		set @sqlData = 'select distinct 企业代码,监测点代码,监测点名称,监测项目编号,监测年,季度,企业类型 from dbo.业务_废气监测数据
						 where 污染物浓度 is not null'+@pollutionWhere
	end
if(@enterType=2)
	begin
		set @sqlData = 'select distinct 企业代码,监测项目编号,监测年,季度 from dbo.业务_污水处理厂监测数据
						where 出口污染物浓度 is not null'+@pollutionWhere
	end
if(@enterType=3)
	begin
		set @sqlData = 'select distinct 企业代码,监测点代码,监测点名称,监测项目编号,监测年,季度,企业类型 from dbo.业务_废水监测数据
						 where 污染物浓度 is not null'+@pollutionWhere+'
						 union all
						 select distinct 企业代码,监测点代码,监测点名称,监测项目编号,监测年,季度,企业类型 from dbo.业务_废气监测数据
						 where 污染物浓度 is not null'+@pollutionWhere
	end

declare @sql nvarchar(max)
set @sql=''
--将监测数据插入到临时表
if(@enterType=1 or @enterType=3)
	begin
		set @sql =
			'select a.*,b.监测点名称,b.监测点代码,c.监测项目编号 as 应测监测项目编号
			into #tmp_PointItem from
			'+@enterTable+' as a inner join ('+@sqlPoint+') as b 
			on a.监测年=b.监测年 and a.企业代码=b.企业代码 and a.企业类型=b.企业类型
			left join 业务_企业监测点污染物信息 as c
			on b.企业代码=c.企业代码 and b.监测点代码=c.监测点代码 and b.监测年=c.监测年 and b.企业类型=c.企业类型
			where 1=1'+@pollutionWhere+';
			
			select a.*,e.监测项目名称,b.监测项目编号 as 实测监测项目编号,case when b.监测项目编号 is null then 0 else 1 end as 是否监测
			into '+@outTable+' from #tmp_PointItem as a
			left join ('+@sqlData+') as b
			on a.企业代码=b.企业代码 and a.监测点代码=b.监测点代码 
			and a.应测监测项目编号=b.监测项目编号 and a.监测年=b.监测年 and a.企业类型=b.企业类型 and a.季度=b.季度
			left join 系统_监测项目 as e
			on a.应测监测项目编号 = e.监测项目编号'
	end
if(@enterType=2)
	begin
		set @sql =
			'select a.*,b.监测项目编号 as 应测监测项目编号
			into #tmp_PointItem from
			'+@enterTable+' as a inner join
			dbo.业务_污水处理厂污染物信息 as b
			on a.企业代码=b.企业代码 and a.监测年=b.监测年
			where 1=1'+@pollutionWhere+';
			
			select a.*,e.监测项目名称,b.监测项目编号 as 实测监测项目编号,case when b.监测项目编号 is null then 0 else 1 end as 是否监测
			into '+@outTable+' from #tmp_PointItem as a
			left join ('+@sqlData+') as b
			on a.企业代码=b.企业代码 and a.应测监测项目编号=b.监测项目编号 and a.监测年=b.监测年 and a.季度=b.季度
			left join 系统_监测项目 as e
			on a.应测监测项目编号 = e.监测项目编号'
	end
exec(@sql)

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetQuarter]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetQuarter]
GO

--==================================================
-- Author:		hanfei
-- Date:		2011-11-27
-- Description:	生成季度临时表，用于统计图和数据上报
--==================================================
Create procedure [dbo].[GetQuarter]
	@beginYear int,		--起始年
	@endYear int,		--结束年
	@beginQuarter int,  --起始季度
	@endQuarter int,	--结束季度
	@outTable nvarchar(50) --输出表名称
as
--处理时间，将时间分为监测年和季度存到临时表中
declare @quarter table(季度 int)
insert into @quarter
select 1 季度 union select 2 季度 union select 3 季度 union select 4 季度
--创建临时表
create table #tmp_Quarter(监测年 int,季度 int)
declare @tempYear int
set @tempYear=@beginYear
while(@tempYear<=@endYear)
	begin
		if(@beginYear<>@endYear)
		begin
			if(@tempYear=@beginYear)
			begin
				insert into #tmp_Quarter select @tempYear as 监测年,季度 from @quarter where 季度>=@beginQuarter
			end
			if(@tempYear=@endYear)
			begin
				insert into #tmp_Quarter select @tempYear as 监测年,季度 from @quarter where 季度<=@endQuarter
			end
			if(@tempYear>@beginYear and @tempYear<@endYear)
			begin
				insert into #tmp_Quarter select @tempYear as 监测年,季度 from @quarter
			end
		end
		else
		begin
			insert into #tmp_Quarter select @tempYear as 监测年,季度 from @quarter where 季度 between @beginQuarter and @endQuarter
		end
		set @tempYear = @tempYear+1
	end
declare @sql nvarchar(max)
set @sql = 'select * into '+@outTable+' from #tmp_Quarter'
exec(@sql)

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Search_SolidMonitor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Search_SolidMonitor]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-10-31
-- Description:	用于固废处理厂的监测情况查询
--==========================================
CREATE procedure [dbo].[Search_SolidMonitor]
	@year nvarchar(4),
	@quarter nvarchar(50),
	@regionCode	nvarchar(max),
	@tradeCode nvarchar(max),
	@pollutionCode nvarchar(max),
	@enterName nvarchar(500),
	@isMonitor nvarchar(1),
	@controlType int,
	@version int
as
begin

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_PointData') is not null begin drop table ##tmp_PointData end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

--设置查询条件
declare @strWhere nvarchar(max) --基本信息查询条件
declare @isMonitorWhere nvarchar(50)	--是否监测查询条件
set @strWhere = ' where 1=1'
set @isMonitorWhere = ' where 1=1'
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@enterName<>'')
	begin
		set @strWhere = @strWhere + ' and 详细名称 like ''%'+@enterName+'%'''
	end
if(@isMonitor<>'')
	begin 
		set @isMonitorWhere = @isMonitorWhere + ' and 是否监测 ='''+@isMonitor+''''
	end
declare @regionStr nvarchar(50)
if(@version=1)--国家
	begin set @regionStr='substring(行政区代码,1,2)+''0000''' end
if(@version=2)--省
	begin set @regionStr='substring(行政区代码,1,4)+''00''' end
if(@version=3)--市
	begin set @regionStr='行政区代码' end
	
declare @sql nvarchar(max)
set @sql=
	'select 季度 into ##tmp_Quarter from (select 1 季度 union select 2 季度 union select 3 季度 union select 4 季度) as t 
	 where 季度 in ('+@quarter+')'
exec (@sql)

--名录库所有企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,企业代码,法人代码,详细名称,企业类型 into ##tmp_MLEnter from V_固废处理厂名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere
exec (@SQL)

--录入企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,企业代码,法人代码,详细名称,企业类型 into ##tmp_LREnter from V_固废处理厂名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere + ' and 基本信息企业代码 is not null'
exec (@SQL)

--获取监测数据
EXEC GetMonitorData 3,@controlType,'##tmp_LREnter',@pollutionCode,'##tmp_ItemData'

--监测项目情况
set @SQL=
	'select 监测年,季度,企业代码,详细名称,企业类型,行政区代码,地区,法人代码,监测点名称,监测项目名称,监测点代码,应测监测项目编号 as 监测项目编号,case when 是否监测>0 then ''是'' else ''否'' end as 是否监测,未监测原因 from 
	(
		select a.*,b.未监测原因 from ##tmp_ItemData as a
		left join 业务_监测项目未监测原因 as b
		on a.是否监测=0 and a.企业代码=b.企业代码 and a.企业类型=b.企业类型 and a.监测年=b.监测年 and a.监测点代码=b.监测点代码 and a.应测监测项目编号=b.监测项目编号 and a.季度=b.季度
		where a.应测监测项目编号 is not null
	) as t'+@isMonitorWhere+'
	order by 季度,行政区代码,详细名称,监测点名称,监测项目名称'
exec(@SQL)

--监测点情况
set @SQL=
	'select 监测年,季度,企业代码,地区,行政区代码,详细名称,法人代码,监测点名称,监测点代码,企业类型,
		case when isnull(sum(是否监测),0)>0 then 1 else 0 end as 是否监测
		into #tmp_监测点情况 from ##tmp_ItemData
		group by 监测年,季度,企业代码,地区,行政区代码,详细名称,法人代码,监测点名称,监测点代码,企业类型;
	select 监测年,季度,地区,行政区代码,企业代码,详细名称,企业类型,法人代码,监测点名称,监测点代码,case when 是否监测>0 then ''是'' else ''否'' end as 是否监测,未监测原因 from 
	(
		select a.*,b.未监测原因 from
		#tmp_监测点情况 as a left join 业务_排口未监测原因 as b
		on a.是否监测=0 and a.企业代码=b.企业代码 and a.企业类型=b.企业类型 and a.监测年=b.监测年 and a.监测点代码=b.监测点代码 and a.季度=b.季度
	) as t'+@isMonitorWhere+'
	order by 季度,行政区代码,详细名称,监测点名称'
exec(@SQL)

--污染源情况
set @SQL=
	'select 监测年,季度,企业代码,地区,行政区代码,详细名称,法人代码,企业类型,
		case when isnull(sum(是否监测),0)>0 then 1 else 0 end as 是否监测
		into #tmp_污染源情况 from 
		(
			select a.*,b.是否监测 from ##tmp_MLEnter as a left join ##tmp_ItemData as b
			on a.企业代码=b.企业代码 and a.监测年=b.监测年 and a.季度=b.季度
		) as t
		group by 监测年,季度,企业代码,地区,行政区代码,详细名称,法人代码,企业类型,企业类型;
	select 监测年,季度,地区,行政区代码,企业代码,详细名称,企业类型,法人代码,case when 是否监测>0 then ''是'' else ''否'' end as 是否监测,未监测原因 from 
	(
		select a.*,b.未监测原因 from
		  #tmp_污染源情况 as a
		  left join 业务_企业未监测原因 as b
		  on a.是否监测=0 and a.企业代码=b.企业代码 and a.监测年=b.监测年 and a.季度=b.季度
	) as t'+@isMonitorWhere+'
	order by 季度,行政区代码,详细名称'
exec(@SQL)

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_PointData') is not null begin drop table ##tmp_PointData end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Search_EnterMonitor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Search_EnterMonitor]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-10-20
-- Description:	用于污染源的监测情况查询
--==========================================
CREATE procedure [dbo].[Search_EnterMonitor]
	@year nvarchar(4),
	@quarter nvarchar(50),
	@regionCode	nvarchar(max),
	@tradeCode nvarchar(max),
	@pollutionCode nvarchar(max),
	@enterName nvarchar(500),
	@isMonitor nvarchar(1),
	@controlType int,
	@version int
as
begin

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_PointData') is not null begin drop table ##tmp_PointData end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

--设置查询条件
declare @strWhere nvarchar(max) --基本信息查询条件
declare @isMonitorWhere nvarchar(50)	--是否监测查询条件
set @strWhere = ' where 1=1'
set @isMonitorWhere = ' where 1=1'
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in ('+@tradeCode+')'
	end
if(@enterName<>'')
	begin
		set @strWhere = @strWhere + ' and 详细名称 like ''%'+@enterName+'%'''
	end
if(@controlType=1)
	begin 
		set @strWhere = @strWhere + ' and 国控类型=1'
	end
if(@controlType=2)
	begin 
		set @strWhere = @strWhere + ' and 国控类型=2'
	end
if(@isMonitor<>'')
	begin 
		set @isMonitorWhere = @isMonitorWhere + ' and 是否监测 ='''+@isMonitor+''''
	end
declare @regionStr nvarchar(50)
if(@version=1)--国家
	begin set @regionStr='substring(行政区代码,1,2)+''0000''' end
if(@version=2)--省
	begin set @regionStr='substring(行政区代码,1,4)+''00''' end
if(@version=3)--市
	begin set @regionStr='行政区代码' end
	
declare @SQL nvarchar(max)
set @SQL=
	'select 季度 into ##tmp_Quarter from (select 1 季度 union select 2 季度 union select 3 季度 union select 4 季度) as t 
	 where 季度 in ('+@quarter+')'
exec (@SQL)

--名录库所有企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,行业代码,行业,企业代码,法人代码,详细名称,企业类型 into ##tmp_MLEnter from V_污染源名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 left join (select 代码编码,代码名称 as 行业 from 系统_代码 where 代码类型编码=15) as c2
	 on 行业代码=c2.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere
exec (@SQL)

--录入企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,行业代码,行业,企业代码,法人代码,详细名称,企业类型 into ##tmp_LREnter from V_污染源名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 left join (select 代码编码,代码名称 as 行业 from 系统_代码 where 代码类型编码=15) as c2
	 on 行业代码=c2.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere + ' and 基本信息企业代码 is not null'
exec (@SQL)

--获取监测数据
EXEC GetMonitorData 1,@controlType,'##tmp_LREnter',@pollutionCode,'##tmp_ItemData'

--监测项目情况
set @SQL=
	'select 监测年,季度,企业代码,详细名称,企业类型,行政区代码,地区,行业,行业代码,法人代码,监测点名称,监测项目名称,监测点代码,应测监测项目编号 as 监测项目编号,case when 是否监测>0 then ''是'' else ''否'' end as 是否监测,未监测原因 from 
	(
		select a.*,b.未监测原因 from ##tmp_ItemData as a
		left join 业务_监测项目未监测原因 as b
		on a.是否监测=0 and a.企业代码=b.企业代码 and a.企业类型=b.企业类型 and a.监测年=b.监测年 and a.监测点代码=b.监测点代码 and a.应测监测项目编号=b.监测项目编号 and a.季度=b.季度
		where a.应测监测项目编号 is not null
	) as t'+@isMonitorWhere+'
	order by 季度,行政区代码,详细名称,监测点名称,监测项目名称'
exec(@SQL)

--监测点情况
set @SQL=
	'select 监测年,季度,企业代码,地区,行政区代码,行业,行业代码,详细名称,法人代码,监测点名称,监测点代码,企业类型,
		case when isnull(sum(是否监测),0)>0 then 1 else 0 end as 是否监测
		into #tmp_监测点情况 from ##tmp_ItemData
		group by 监测年,季度,企业代码,地区,行政区代码,行业,行业代码,详细名称,法人代码,监测点名称,监测点代码,企业类型;
	select 监测年,季度,地区,行政区代码,行业,行业代码,企业代码,详细名称,企业类型,法人代码,监测点名称,监测点代码,case when 是否监测>0 then ''是'' else ''否'' end as 是否监测,未监测原因 from 
	(
		select a.*,b.未监测原因 from
		#tmp_监测点情况 as a left join 业务_排口未监测原因 as b
		on a.是否监测=0 and a.企业代码=b.企业代码 and a.企业类型=b.企业类型 and a.监测年=b.监测年 and a.监测点代码=b.监测点代码 and a.季度=b.季度
	) as t'+@isMonitorWhere+'
	order by 季度,行政区代码,详细名称,监测点名称'
exec(@SQL)

--污染源情况
set @SQL=
	'select 监测年,季度,企业代码,地区,行政区代码,行业,行业代码,详细名称,法人代码,企业类型,
		case when isnull(sum(是否监测),0)>0 then 1 else 0 end as 是否监测
		into #tmp_污染源情况 from 
		(
			select a.*,b.是否监测 from ##tmp_MLEnter as a left join ##tmp_ItemData as b
			on a.企业代码=b.企业代码 and a.监测年=b.监测年 and a.季度=b.季度
		) as t
		group by 监测年,季度,企业代码,地区,行政区代码,行业,行业代码,详细名称,法人代码,企业类型,企业类型;
	select 监测年,季度,地区,行政区代码,行业,行业代码,企业代码,详细名称,企业类型,法人代码,case when 是否监测>0 then ''是'' else ''否'' end as 是否监测,未监测原因 from 
	(
		select a.*,b.未监测原因 from
		  #tmp_污染源情况 as a
		  left join 业务_企业未监测原因 as b
		  on a.是否监测=0 and a.企业代码=b.企业代码 and a.监测年=b.监测年 and a.季度=b.季度
	) as t'+@isMonitorWhere+'
	order by 季度,行政区代码,详细名称'
exec(@SQL)

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_PointData') is not null begin drop table ##tmp_PointData end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Search_WaterMonitor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Search_WaterMonitor]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-10-31
-- Description:	用于污水处理厂的监测情况查询
--==========================================
CREATE procedure [dbo].[Search_WaterMonitor]
	@year nvarchar(4),
	@quarter nvarchar(50),
	@regionCode	nvarchar(max),
	@tradeCode nvarchar(max),
	@pollutionCode nvarchar(max),
	@enterName nvarchar(500),
	@isMonitor nvarchar(1),
	@controlType int,
	@version int
as
begin

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_PointData') is not null begin drop table ##tmp_PointData end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

--设置查询条件
declare @strWhere nvarchar(max) --基本信息查询条件
declare @isMonitorWhere nvarchar(50)	--是否监测查询条件
set @strWhere = ' where 1=1'
set @isMonitorWhere = ' where 1=1'
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@enterName<>'')
	begin
		set @strWhere = @strWhere + ' and 详细名称 like ''%'+@enterName+'%'''
	end
if(@isMonitor<>'')
	begin 
		set @isMonitorWhere = @isMonitorWhere + ' and 是否监测 ='''+@isMonitor+''''
	end
declare @regionStr nvarchar(50)
if(@version=1)--国家
	begin set @regionStr='substring(行政区代码,1,2)+''0000''' end
if(@version=2)--省
	begin set @regionStr='substring(行政区代码,1,4)+''00''' end
if(@version=3)--市
	begin set @regionStr='行政区代码' end
	
declare @sql nvarchar(max)
set @sql=
	'select 季度 into ##tmp_Quarter from (select 1 季度 union select 2 季度 union select 3 季度 union select 4 季度) as t 
	 where 季度 in ('+@quarter+')'
exec (@sql)

--名录库所有企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,企业代码,法人代码,详细名称,企业类型 into ##tmp_MLEnter from V_污水处理厂名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere
exec (@SQL)
		
--录入企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,企业代码,法人代码,详细名称,企业类型 into ##tmp_LREnter from V_污水处理厂名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere + ' and 基本信息企业代码 is not null'
exec (@SQL)
	
--获取监测数据
EXEC GetMonitorData 2,@controlType,'##tmp_LREnter',@pollutionCode,'##tmp_ItemData'

--监测项目情况
set @SQL=
	'select 监测年,季度,企业代码,详细名称,企业类型,行政区代码,地区,法人代码,'''' as 监测点代码,应测监测项目编号 as 监测项目编号,监测项目名称,case when 是否监测>0 then ''是'' else ''否'' end as 是否监测,未监测原因 from 
	(
		select a.*,b.未监测原因 from ##tmp_ItemData as a
		left join 业务_监测项目未监测原因 as b
		on a.是否监测=0 and a.企业代码=b.企业代码 and a.企业类型=b.企业类型 and a.监测年=b.监测年 and a.应测监测项目编号=b.监测项目编号 and a.季度=b.季度
	) as t'+@isMonitorWhere+'
	order by 季度,行政区代码,详细名称,监测项目名称'
exec(@SQL)

--污染源情况
set @SQL=
	'select 监测年,季度,企业代码,地区,行政区代码,详细名称,法人代码,企业类型,
	case when isnull(sum(是否监测),0)>0 then 1 else 0 end as 是否监测
	into #tmp_污染源情况 from 
		(
			select a.*,b.是否监测 from ##tmp_MLEnter as a left join ##tmp_ItemData as b
			on a.企业代码=b.企业代码 and a.监测年=b.监测年 and a.季度=b.季度
		) as t
	group by 监测年,季度,企业代码,地区,行政区代码,详细名称,法人代码,企业类型;
	select 监测年,季度,地区,行政区代码,企业代码,详细名称,企业类型,法人代码,case when 是否监测>0 then ''是'' else ''否'' end as 是否监测,未监测原因 from 
	(
		select a.*,b.未监测原因 from
		#tmp_污染源情况 as a
		left join 业务_企业未监测原因 as b
		on a.是否监测=0 and a.企业代码=b.企业代码 and a.企业类型=b.企业类型 and a.监测年=b.监测年 and a.季度=b.季度
	) t'+@isMonitorWhere+'
	order by 季度,行政区代码,详细名称'
exec(@SQL)

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_PointData') is not null begin drop table ##tmp_PointData end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Stat_SolidMonitor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Stat_SolidMonitor]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-10-20
-- Description:	用于固废处理厂的监测情况统计
--==========================================
CREATE procedure [dbo].[Stat_SolidMonitor]
	@year nvarchar(4),
	@quarter nvarchar(50),
	@regionCode	nvarchar(max),
	@enterName nvarchar(500),
	@pollutionCode nvarchar(max),
	@controlType int,
	@version int
as
begin

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_PointData') is not null begin drop table ##tmp_PointData end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

declare @strWhere nvarchar(max) --基本信息查询条件
set @strWhere = ' where 1=1'
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@enterName<>'')
	begin
		set @strWhere = @strWhere + ' and 详细名称 like ''%'+@enterName+'%'''
	end
declare @regionStr nvarchar(50)
if(@version=1)--国家
	begin set @regionStr='substring(行政区代码,1,2)+''0000''' end
if(@version=2)--省
	begin set @regionStr='substring(行政区代码,1,4)+''00''' end
if(@version=3)--市
	begin set @regionStr='行政区代码' end

declare @SQL nvarchar(max)
set @SQL=
	'select 季度 into ##tmp_Quarter from (select 1 季度 union select 2 季度 union select 3 季度 union select 4 季度) as t 
	 where 季度 in ('+@quarter+')'
exec (@SQL)

--名录库所有企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,企业代码,法人代码,详细名称,企业类型 into ##tmp_MLEnter from V_固废处理厂名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere
exec (@SQL)
		
--录入企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,企业代码,法人代码,详细名称,企业类型 into ##tmp_LREnter from V_固废处理厂名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere + ' and 基本信息企业代码 is not null'
exec (@SQL)

--获取监测数据
EXEC GetMonitorData 3,@controlType,'##tmp_LREnter',@pollutionCode,'##tmp_ItemData'

--监测点监测项目统计
set @SQL=
   'select 监测年,季度,地区,行政区代码,企业代码,详细名称,法人代码,监测点名称,应测项目数,实测项目数,
	case when isnull(应测项目数,0)=0 then 0.00 else cast(cast(实测项目数 as decimal(18,4))/应测项目数 as decimal(18,4)) end as 实际监测率
	into ##tmp_PointData from (
		select 监测年,季度,地区,行政区代码,企业代码,详细名称,法人代码,监测点名称,count(应测监测项目编号) as 应测项目数,sum(是否监测) as 实测项目数
		from ##tmp_ItemData
		group by 监测年,季度,地区,行政区代码,企业代码,详细名称,法人代码,监测点名称
	) as t
	select * from ##tmp_PointData order by 监测年,季度,行政区代码,详细名称,监测点名称'
exec (@SQL)

--污染源监测项目统计
set @SQL=
		'select 监测年,季度,地区,行政区代码,详细名称,法人代码,count(监测点名称) as 监测点数,sum(应测项目数) as 应测项目数,sum(实测项目数) as 实测项目数,cast(avg(实际监测率) as decimal(18,4)) as 实际监测率 
		from 
		(
			select a.*,b.监测点名称,isnull(b.应测项目数,0) as 应测项目数,isnull(b.实测项目数,0) as 实测项目数,isnull(b.实际监测率,0) as 实际监测率 from ##tmp_MLEnter as a left join ##tmp_PointData as b
			on a.企业代码=b.企业代码 and a.监测年=b.监测年 and a.季度=b.季度
		) as t
		group by 监测年,季度,地区,行政区代码,详细名称,法人代码
		order by 监测年,季度,行政区代码,详细名称'
exec (@SQL)

--行政区监测项目统计
set @SQL=
   'select 监测年,季度,地区,行政区代码,应测企业数,isnull(应测项目数,0) as 应测项目数,isnull(实测企业数,0) as 实测企业数,isnull(实测监测点数,0) as 实测监测点数,isnull(实测项目数,0) as 实测项目数,isnull(可测企业数,0) as 可测企业数,isnull(可测监测点数,0) as 可测监测点数,
	case when isnull(应测项目数,0)=0 then 0 else cast(cast(isnull(实测项目数,0) as decimal(18,4))/应测项目数 as decimal(18,4)) end as 实际监测率 from 
   (
		select z.监测年,z.季度,z.行政区代码,z.地区,z.应测企业数,a.应测项目数,b.实测企业数,b.实测监测点数,b.实测项目数,c.可测企业数,c.可测监测点数 from
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,地区,count(企业代码) as 应测企业数 from ##tmp_MLEnter
			group by 监测年,季度,'+@regionStr+',地区
		) as z
		left join
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,sum(应测项目数) as 应测项目数 from ##tmp_PointData
			group by 监测年,季度,'+@regionStr+'
		) as a on a.行政区代码=z.行政区代码 and a.监测年=z.监测年 and a.季度=z.季度
		left join
		(
			select 监测年,季度,行政区代码,count(distinct 企业代码) as 实测企业数,sum(实测监测点数) as 实测监测点数,sum(实测项目数) as 实测项目数
			from
			(
				select 监测年,季度,'+@regionStr+' as 行政区代码,企业代码,count(distinct 监测点名称) as 实测监测点数,sum(实测项目数) as 实测项目数
				from ##tmp_PointData where 实测项目数>0
				group by 监测年,季度,'+@regionStr+',企业代码
			) as t group by 监测年,季度,行政区代码
		) as b on z.行政区代码=b.行政区代码 and z.监测年=b.监测年 and z.季度=b.季度
		left join
		(
			select 监测年,季度,行政区代码,count(distinct 企业代码) as 可测企业数,sum(可测监测点数) as 可测监测点数
			from
			(
				select 监测年,季度,'+@regionStr+' as 行政区代码,企业代码,sum(可测监测点数) as 可测监测点数
				from (
					select a.*,c.未监测原因 from 
					(
						select a.监测年,a.季度,a.行政区代码,a.企业代码,b.可测监测点数,b.实测项目数 from ##tmp_MLEnter as a left join 
						(select 监测年,季度,行政区代码,企业代码,count(监测点名称) as 可测监测点数,sum(实测项目数) as 实测项目数 from ##tmp_PointData
						group by 监测年,季度,行政区代码,企业代码) as b
						on a.监测年=b.监测年 and a.季度=b.季度 and a.企业代码=b.企业代码
					) as a
					left join dbo.业务_企业未监测原因 as c
					on a.企业代码=c.企业代码 and c.监测年=a.监测年 and c.季度=a.季度 and isnull(a.实测项目数,0)=0
					where isnull(c.未监测原因,'''') not in (''季节性停产'',''永久性关停'',''名单下错'',''不具备监测条件'',''其他原因'')) as t
				group by 监测年,季度,'+@regionStr+',企业代码
			) as t group by 监测年,季度,行政区代码
		) as c on z.行政区代码=c.行政区代码 and z.监测年=c.监测年 and z.季度=c.季度
	) as a
	order by 监测年,季度,行政区代码'
exec (@SQL)

--行政区污染源统计
set @SQL=
   'select 监测年,季度,地区,行政区代码,应测企业数,isnull(实测企业数,0) as 实测企业数,isnull(可测企业数,0) as 可测企业数,
   case when isnull(可测企业数,0)=0 then 0.00 else cast(cast(isnull(实测企业数,0) as decimal(18,4))/可测企业数 as decimal(18,4)) end as 可监测率  from
	(
		select a.监测年,a.季度,a.行政区代码,a.地区,a.应测企业数,b.实测企业数,c.可测企业数 from
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,地区,count(distinct 企业代码) as 应测企业数 from ##tmp_MLEnter
			group by 监测年,季度,'+@regionStr+',地区
		) as a
		left join
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,count(distinct 企业代码) as 实测企业数 from ##tmp_PointData
			where 实测项目数>0
			group by 监测年,季度,'+@regionStr+'
		) as b on a.行政区代码=b.行政区代码 and a.监测年=b.监测年 and a.季度=b.季度
		left join
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,count(distinct 企业代码) as 可测企业数
				from (
					select a.* from 
					(
						select a.监测年,a.季度,a.行政区代码,a.企业代码,b.实测项目数 from ##tmp_MLEnter as a left join 
						(select 监测年,季度,行政区代码,企业代码,sum(实测项目数) as 实测项目数 from ##tmp_PointData
						group by 监测年,季度,行政区代码,企业代码) as b
						on a.监测年=b.监测年 and a.季度=b.季度 and a.企业代码=b.企业代码
					) as a
					left join dbo.业务_企业未监测原因 as c
					on a.企业代码=c.企业代码 and c.监测年=a.监测年 and c.季度=a.季度 and isnull(a.实测项目数,0)=0
					where isnull(c.未监测原因,'''') not in (''季节性停产'',''永久性关停'',''名单下错'',''不具备监测条件'',''其他原因'')) as t
				group by 监测年,季度,'+@regionStr+'
		) as c on a.行政区代码=c.行政区代码 and a.监测年=c.监测年 and a.季度=c.季度
	) as a
	order by 监测年,季度,行政区代码'
exec (@SQL)

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_PointData') is not null begin drop table ##tmp_PointData end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Stat_EnterMonitor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Stat_EnterMonitor]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-10-20
-- Description:	用于污染源的监测情况统计
--==========================================
CREATE procedure [dbo].[Stat_EnterMonitor]
	@year nvarchar(4),
	@quarter nvarchar(50),
	@regionCode	nvarchar(max),
	@enterName nvarchar(500),
	@pollutionCode nvarchar(max),
	@controlType int,
	@version int
as
begin

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_PointData') is not null begin drop table ##tmp_PointData end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

declare @strWhere nvarchar(max) --基本信息查询条件
set @strWhere = ' where 1=1'
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@enterName<>'')
	begin
		set @strWhere = @strWhere + ' and 详细名称 like ''%'+@enterName+'%'''
	end
if(@controlType=1)
	begin 
		set @strWhere = @strWhere + ' and 国控类型=1'
	end
if(@controlType=2)
	begin 
		set @strWhere = @strWhere + ' and 国控类型=2'
	end
declare @regionStr nvarchar(50)
if(@version=1)--国家
	begin set @regionStr='substring(行政区代码,1,2)+''0000''' end
if(@version=2)--省
	begin set @regionStr='substring(行政区代码,1,4)+''00''' end
if(@version=3)--市
	begin set @regionStr='行政区代码' end

declare @SQL nvarchar(max)
set @SQL=
	'select 季度 into ##tmp_Quarter from (select 1 季度 union select 2 季度 union select 3 季度 union select 4 季度) as t 
	 where 季度 in ('+@quarter+')'
exec (@SQL)

--名录库所有企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,企业代码,法人代码,详细名称,企业类型 into ##tmp_MLEnter from V_污染源名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere
exec (@SQL)
		
--录入企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,企业代码,法人代码,详细名称,企业类型 into ##tmp_LREnter from V_污染源名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere + ' and 基本信息企业代码 is not null'
exec (@SQL)

--获取监测数据
EXEC GetMonitorData 1,@controlType,'##tmp_LREnter',@pollutionCode,'##tmp_ItemData'

--监测点监测项目统计
set @SQL=
   'select 监测年,季度,地区,行政区代码,企业代码,详细名称,法人代码,监测点名称,应测项目数,实测项目数,
    case when isnull(应测项目数,0)=0 then 0.00 else cast(cast(实测项目数 as decimal(18,4))/应测项目数 as decimal(18,4)) end as 实际监测率 
	into ##tmp_PointData from (
		select 监测年,季度,地区,行政区代码,企业代码,详细名称,法人代码,监测点名称,count(应测监测项目编号) as 应测项目数,sum(是否监测) as 实测项目数
		from ##tmp_ItemData
		group by 监测年,季度,地区,行政区代码,企业代码,详细名称,法人代码,监测点名称
	) as t
	select * from ##tmp_PointData order by 监测年,季度,行政区代码,详细名称,监测点名称'
exec (@SQL)

--污染源监测项目统计
set @SQL=
		'select 监测年,季度,地区,行政区代码,详细名称,法人代码,count(监测点名称) as 监测点数,sum(应测项目数) as 应测项目数,sum(实测项目数) as 实测项目数,cast(avg(实际监测率) as decimal(18,4)) as 实际监测率 
		from 
		(
			select a.*,b.监测点名称,isnull(b.应测项目数,0) as 应测项目数,isnull(b.实测项目数,0) as 实测项目数,isnull(b.实际监测率,0) as 实际监测率 from ##tmp_MLEnter as a left join ##tmp_PointData as b
			on a.企业代码=b.企业代码 and a.监测年=b.监测年 and a.季度=b.季度
		) as t
		group by 监测年,季度,地区,行政区代码,详细名称,法人代码
		order by 监测年,季度,行政区代码,详细名称'
exec (@SQL)

--行政区监测项目统计
set @SQL=
   'select 监测年,季度,地区,行政区代码,应测企业数,isnull(应测项目数,0) as 应测项目数,isnull(实测企业数,0) as 实测企业数,isnull(实测监测点数,0) as 实测监测点数,isnull(实测项目数,0) as 实测项目数,isnull(可测企业数,0) as 可测企业数,isnull(可测监测点数,0) as 可测监测点数,
	case when isnull(应测项目数,0)=0 then 0 else cast(cast(isnull(实测项目数,0) as decimal(18,4))/应测项目数 as decimal(18,4)) end as 实际监测率 from 
   (
		select z.监测年,z.季度,z.行政区代码,z.地区,z.应测企业数,a.应测项目数,b.实测企业数,b.实测监测点数,b.实测项目数,c.可测企业数,c.可测监测点数 from
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,地区,count(企业代码) as 应测企业数 from ##tmp_MLEnter
			group by 监测年,季度,'+@regionStr+',地区
		) as z
		left join
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,sum(应测项目数) as 应测项目数 from ##tmp_PointData
			group by 监测年,季度,'+@regionStr+'
		) as a on a.行政区代码=z.行政区代码 and a.监测年=z.监测年 and a.季度=z.季度
		left join
		(
			select 监测年,季度,行政区代码,count(distinct 企业代码) as 实测企业数,sum(实测监测点数) as 实测监测点数,sum(实测项目数) as 实测项目数
			from
			(
				select 监测年,季度,'+@regionStr+' as 行政区代码,企业代码,count(distinct 监测点名称) as 实测监测点数,sum(实测项目数) as 实测项目数
				from ##tmp_PointData where 实测项目数>0
				group by 监测年,季度,'+@regionStr+',企业代码
			) as t group by 监测年,季度,行政区代码
		) as b on z.行政区代码=b.行政区代码 and z.监测年=b.监测年 and z.季度=b.季度
		left join
		(
			select 监测年,季度,行政区代码,count(distinct 企业代码) as 可测企业数,sum(可测监测点数) as 可测监测点数
			from
			(
				select 监测年,季度,'+@regionStr+' as 行政区代码,企业代码,sum(可测监测点数) as 可测监测点数
				from (
					select a.*,c.未监测原因 from 
					(
						select a.监测年,a.季度,a.行政区代码,a.企业代码,b.可测监测点数,b.实测项目数 from ##tmp_MLEnter as a left join 
						(select 监测年,季度,行政区代码,企业代码,count(监测点名称) as 可测监测点数,sum(实测项目数) as 实测项目数 from ##tmp_PointData
						group by 监测年,季度,行政区代码,企业代码) as b
						on a.监测年=b.监测年 and a.季度=b.季度 and a.企业代码=b.企业代码
					) as a
					left join dbo.业务_企业未监测原因 as c
					on a.企业代码=c.企业代码 and c.监测年=a.监测年 and c.季度=a.季度 and isnull(a.实测项目数,0)=0
					where isnull(c.未监测原因,'''') not in (''季节性停产'',''永久性关停'',''名单下错'',''不具备监测条件'',''其他原因'')) as t
				group by 监测年,季度,'+@regionStr+',企业代码
			) as t group by 监测年,季度,行政区代码
		) as c on z.行政区代码=c.行政区代码 and z.监测年=c.监测年 and z.季度=c.季度
	) as a
	order by 监测年,季度,行政区代码'
exec (@SQL)

--行政区污染源统计
set @SQL=
   'select 监测年,季度,地区,行政区代码,应测企业数,isnull(实测企业数,0) as 实测企业数,isnull(可测企业数,0) as 可测企业数,
   case when isnull(可测企业数,0)=0 then 0.00 else cast(cast(isnull(实测企业数,0) as decimal(18,4))/可测企业数 as decimal(18,4)) end as 可监测率  from
	(
		select a.监测年,a.季度,a.行政区代码,a.地区,a.应测企业数,b.实测企业数,c.可测企业数 from
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,地区,count(distinct 企业代码) as 应测企业数 from ##tmp_MLEnter
			group by 监测年,季度,'+@regionStr+',地区
		) as a
		left join
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,count(distinct 企业代码) as 实测企业数 from ##tmp_PointData
			where 实测项目数>0
			group by 监测年,季度,'+@regionStr+'
		) as b on a.行政区代码=b.行政区代码 and a.监测年=b.监测年 and a.季度=b.季度
		left join
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,count(distinct 企业代码) as 可测企业数
				from (
					select a.* from 
					(
						select a.监测年,a.季度,a.行政区代码,a.企业代码,b.实测项目数 from ##tmp_MLEnter as a left join 
						(select 监测年,季度,行政区代码,企业代码,sum(实测项目数) as 实测项目数 from ##tmp_PointData
						group by 监测年,季度,行政区代码,企业代码) as b
						on a.监测年=b.监测年 and a.季度=b.季度 and a.企业代码=b.企业代码
					) as a
					left join dbo.业务_企业未监测原因 as c
					on a.企业代码=c.企业代码 and c.监测年=a.监测年 and c.季度=a.季度 and isnull(a.实测项目数,0)=0
					where isnull(c.未监测原因,'''') not in (''季节性停产'',''永久性关停'',''名单下错'',''不具备监测条件'',''其他原因'')) as t
				group by 监测年,季度,'+@regionStr+'
		) as c on a.行政区代码=c.行政区代码 and a.监测年=c.监测年 and a.季度=c.季度
	) as a
	order by 监测年,季度,行政区代码'
exec (@SQL)

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_PointData') is not null begin drop table ##tmp_PointData end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

end

GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Stat_WaterMonitor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Stat_WaterMonitor]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-10-20
-- Description:	用于污水处理厂的监测情况统计
--==========================================
CREATE procedure [dbo].[Stat_WaterMonitor]
	@year nvarchar(4),
	@quarter nvarchar(50),
	@regionCode	nvarchar(max),
	@enterName nvarchar(500),
	@pollutionCode nvarchar(max),
	@controlType int,
	@version int
as
begin

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_PointData') is not null begin drop table ##tmp_PointData end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

--设置查询条件
declare @strWhere nvarchar(max) --基本信息查询条件
declare @isMonitorWhere nvarchar(50)	--是否监测查询条件
set @strWhere = ' where 1=1'
set @isMonitorWhere = ' where 1=1'
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@enterName<>'')
	begin
		set @strWhere = @strWhere + ' and 详细名称 like ''%'+@enterName+'%'''
	end
declare @regionStr nvarchar(50)
if(@version=1)--国家
	begin set @regionStr='substring(行政区代码,1,2)+''0000''' end
if(@version=2)--省
	begin set @regionStr='substring(行政区代码,1,4)+''00''' end
if(@version=3)--市
	begin set @regionStr='行政区代码' end

declare @SQL nvarchar(max)
set @SQL=
	'select 季度 into ##tmp_Quarter from (select 1 季度 union select 2 季度 union select 3 季度 union select 4 季度) as t 
	 where 季度 in ('+@quarter+')'
exec (@SQL)

--名录库所有企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,企业代码,法人代码,详细名称,企业类型 into ##tmp_MLEnter from V_污水处理厂名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere
exec (@SQL)
		
--录入企业
set @SQL=
	'select 监测年,季度,行政区代码,地区,企业代码,法人代码,详细名称,企业类型 into ##tmp_LREnter from V_污水处理厂名录库
	 left join (select 代码编码,代码名称 as 地区 from 系统_代码 where 代码类型编码=14) as t1
	 on '+@regionStr+'=t1.代码编码
	 cross join (select 季度 from ##tmp_Quarter) as q' + @strWhere + ' and 基本信息企业代码 is not null'
exec (@SQL)
	
--获取监测数据
EXEC GetMonitorData 2,@controlType,'##tmp_LREnter',@pollutionCode,'##tmp_ItemData'

--污染源监测项目统计
set @SQL=
   'select 监测年,季度,行政区代码,地区,详细名称,法人代码,应测项目数,isnull(实测项目数,0) as 实测项目数,case when isnull(实测项目数,0)=0 then 0 else cast(cast(实测项目数 as decimal(18,4))/应测项目数 as decimal(18,4)) end as 实际监测率 from 
	(
		select a.监测年,a.季度,a.行政区代码,a.地区,a.详细名称,a.法人代码,count(应测监测项目编号) as 应测项目数,sum(是否监测) as 实测项目数 
		from ##tmp_MLEnter as a left join ##tmp_ItemData as b
		on a.企业代码=b.企业代码 and a.监测年=b.监测年 and a.季度=b.季度
		group by a.监测年,a.季度,a.行政区代码,a.地区,a.详细名称,a.法人代码
	) as a
	order by 监测年,季度,行政区代码,详细名称'
exec (@SQL)

--行政区监测项目统计
set @SQL=
   'select 监测年,季度,地区,行政区代码,应测企业数,isnull(应测项目数,0) as 应测项目数,isnull(实测企业数,0) as 实测企业数,isnull(实测项目数,0) as 实测项目数,isnull(可测企业数,0) as 可测企业数,
   case when isnull(应测项目数,0)=0 then 0 else cast(cast(isnull(实测项目数,0) as decimal(18,4))/应测项目数 as decimal(18,4)) end as 实际监测率 from 
   (
		select z.监测年,z.季度,z.行政区代码,z.地区,z.应测企业数,a.应测项目数,b.实测企业数,b.实测项目数,c.可测企业数 from
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,地区,count(企业代码) as 应测企业数 from ##tmp_MLEnter
			group by 监测年,季度,'+@regionStr+',地区
		) as z
		left join
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,count(应测监测项目编号) as 应测项目数 from ##tmp_ItemData
			group by 监测年,季度,'+@regionStr+'
		) as a on a.行政区代码=z.行政区代码 and a.监测年=z.监测年 and a.季度=z.季度
		left join
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,count(distinct 企业代码) as 实测企业数,sum(是否监测) as 实测项目数 
			from ##tmp_ItemData where 实测监测项目编号 is not null 
			group by 监测年,季度,'+@regionStr+'
		) as b on z.行政区代码=b.行政区代码 and z.监测年=b.监测年 and z.季度=b.季度
		left join
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,count(distinct 企业代码) as 可测企业数
			from
			(
				select a.* from 
				(
					select a.监测年,a.季度,a.行政区代码,a.企业代码,实测项目数 from ##tmp_MLEnter as a left join 
					(select 监测年,季度,行政区代码,企业代码,sum(是否监测) as 实测项目数 from ##tmp_ItemData
					group by 监测年,季度,行政区代码,企业代码) as b
					on a.监测年=b.监测年 and a.季度=b.季度 and a.企业代码=b.企业代码
				) as a
				left join dbo.业务_企业未监测原因 as c
				on a.企业代码=c.企业代码 and c.监测年=a.监测年 and c.季度=a.季度 and isnull(a.实测项目数,0)=0
				where isnull(c.未监测原因,'''') not in (''季节性停产'',''永久性关停'',''名单下错'',''不具备监测条件'',''其他原因'')) as t
				group by 监测年,季度,'+@regionStr+'
		) as c on z.行政区代码=c.行政区代码 and z.监测年=c.监测年 and z.季度=c.季度
	) as a
	order by 监测年,季度,行政区代码'
exec (@SQL)

--行政区污染源统计
set @SQL=
  'select 监测年,季度,地区,行政区代码,应测企业数,isnull(可测企业数,0) as 可测企业数,isnull(实测企业数,0) as 实测企业数,
  case when isnull(可测企业数,0)=0 then 0.00 else cast(cast(isnull(实测企业数,0) as decimal(18,4))/可测企业数 as decimal(18,4)) end as 可监测率 from
   (
		select a.监测年,a.季度,a.行政区代码,a.地区,a.应测企业数,b.实测企业数,c.可测企业数 from
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,地区,count(企业代码) as 应测企业数 from ##tmp_MLEnter
			group by 监测年,季度,'+@regionStr+',地区
		) as a
		left join
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,count(distinct 企业代码) as 实测企业数 
			from ##tmp_ItemData where 实测监测项目编号 is not null 
			group by 监测年,季度,'+@regionStr+'
		) as b on a.行政区代码=b.行政区代码 and a.监测年=b.监测年 and a.季度=b.季度
		left join
		(
			select 监测年,季度,'+@regionStr+' as 行政区代码,count(distinct 企业代码) as 可测企业数
			from
			(
				select a.* from 
					(
						select a.监测年,a.季度,a.行政区代码,a.企业代码,b.实测项目数 from ##tmp_MLEnter as a left join 
						(select 监测年,季度,行政区代码,企业代码,sum(是否监测) as 实测项目数 from ##tmp_ItemData
						group by 监测年,季度,行政区代码,企业代码) as b
						on a.监测年=b.监测年 and a.季度=b.季度 and a.企业代码=b.企业代码
					) as a
					left join dbo.业务_企业未监测原因 as c
					on a.企业代码=c.企业代码 and c.监测年=a.监测年 and c.季度=a.季度 and isnull(a.实测项目数,0)=0
					where isnull(c.未监测原因,'''') not in (''季节性停产'',''永久性关停'',''名单下错'',''不具备监测条件'',''其他原因'')) as t
				group by 监测年,季度,'+@regionStr+'
		) as c on a.行政区代码=c.行政区代码 and a.监测年=c.监测年 and a.季度=c.季度
	) as a
	order by 监测年,季度,行政区代码'
exec (@SQL)

if object_id('tempdb..##tmp_MLEnter') is not null begin drop table ##tmp_MLEnter end
if object_id('tempdb..##tmp_LREnter') is not null begin drop table ##tmp_LREnter end
if object_id('tempdb..##tmp_ItemData') is not null begin drop table ##tmp_ItemData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

end

GO

/****** Object:  StoredProcedure [dbo].[StatGas]    Script Date: 10/27/2011 15:45:56 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[StatGas]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[StatGas]
GO

--=======================================================
-- Author:      hanfei
-- Date:        2011-10-27
-- Description: 废气录入情况统计
-- 参数@type:   计算标准 1:按(省/市)统计,2:按省统计,3:按专题区域统计
-- 参数@controlType: 控制级别 1:国控,2:省控,3:市控
--=======================================================
Create proc [dbo].[StatGas]
(
	@type int,
	@year varchar(4),
	@quarter varchar(50),
	@regionCode varchar(max),
	@projectArea varchar(max),
	@controlType varchar(1)
)
as
begin
--设置查询条件
declare @strWhere nvarchar(max) --基本信息查询条件
declare @mlkWhere nvarchar(max)--名录库查询条件
set @strWhere = ' where 1=1 '
set @mlkWhere = ' where 1=1 and 企业类型=1 '
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
		set @mlkWhere = @mlkWhere + ' and 年度 = '+ @year
	end
if(@quarter<>'')
	begin
		set @strWhere = @strWhere + ' and 季度 in ('+ @quarter +')'
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @mlkWhere = @mlkWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@type=3 and @projectArea<>'')
	begin
		set @strWhere = @strWhere + ' and 区域类型编码 in ('+@projectArea+')'
	end
if(@controlType<>0)
	begin
		set @strWhere =  @strWhere + ' and (污染源控制级别代码气 = ' + @controlType
								   + ' and 污染源控制级别代码 >= ' + @controlType +')'
		set @mlkWhere = @mlkWhere + ' and (污染源控制级别代码气 = ' + @controlType
								   + ' and 污染源控制级别代码 >= ' + @controlType +')'
	end

declare @sql nvarchar(max)
--按(省/市)统计
if @type=1
begin
	set @sql=
		'select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,2)+''0000''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_废气统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,2)+''0000''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		union all
		select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,4)+''00'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,4)+''00''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,4)+''00'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_废气统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,4)+''00''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		order by 代码编码'
end
--按省统计
if(@type=2)	
begin
	set @sql=
		'select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,2)+''0000''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_废气统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,2)+''0000''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		order by 代码编码'
end
--按专题区域统计
if(@type=3)
begin
	set @sql=
		'select c.代码编码 as 专题区域代码,c.代码名称 as 专题区域,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,区域类型编码 from
			(
				select 企业代码,区域类型编码 from 业务_污染源名录库_总站 as a
				inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
				'+@mlkWhere+' group by 企业代码,行政区代码,区域类型编码
			) as t
			group by 区域类型编码
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,区域类型编码 from
			(
				select 企业代码,区域类型编码 from V_废气统计 as a
				inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
				'+@strWhere+' group by 企业代码,行政区代码,区域类型编码
			) as t
			group by 区域类型编码
		) as b
		on a.区域类型编码=b.区域类型编码
		inner join 系统_代码 as c
		on c.代码类型编码=33 and a.区域类型编码=c.ID
		order by 代码编码'
end
exec(@sql)
end
go

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[StatWhater]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[StatWhater]
GO

--=======================================================
-- Author:      hanfei
-- Date:        2011-10-27
-- Description: 废水录入情况统计
-- 变量@type: 1:按(省/市)统计,2:按省统计,3:按专题区域统计
--=======================================================
CREATE proc [dbo].[StatWhater]
(
	@type int,
	@year varchar(4),
	@quarter varchar(50),
	@regionCode varchar(max),
	@projectArea varchar(max),
	@controlType varchar(1)
)
as
begin
--设置查询条件
declare @strWhere nvarchar(max) --基本信息查询条件
declare @mlkWhere nvarchar(max)--名录库查询条件
set @strWhere = ' where 1=1 '
set @mlkWhere = ' where 1=1 and 企业类型=1 '
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
		set @mlkWhere = @mlkWhere + ' and 年度 = '+ @year
	end
if(@quarter<>'')
	begin
		set @strWhere = @strWhere + ' and 季度 in ('+ @quarter +')'
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @mlkWhere = @mlkWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@type=3 and @projectArea<>'')
	begin
		set @strWhere = @strWhere + ' and 区域类型编码 in ('+@projectArea+')'
	end
if(@controlType<>0)
	begin
		set @strWhere =  @strWhere + ' and (污染源控制级别代码 = ' + @controlType
								   + ' and 污染源控制级别代码气 >= ' + @controlType +')'
		set @mlkWhere = @mlkWhere + ' and (污染源控制级别代码 = ' + @controlType
								   + ' and 污染源控制级别代码气 >= ' + @controlType +')'
	end
	
declare @sql nvarchar(max)
--按(省/市)统计
if @type=1
begin
	set @sql=
		'select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,2)+''0000''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_废水统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,2)+''0000''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		union all
		select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,4)+''00'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,4)+''00''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,4)+''00'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_废水统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,4)+''00''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		order by 代码编码'
end
--按省统计
if(@type=2)	
begin
	set @sql=
		'select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,2)+''0000''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_废水统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,2)+''0000''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		order by 代码编码'
end
--按专题区域统计
if(@type=3)
begin
	set @sql=
		'select c.代码编码 as 专题区域代码,c.代码名称 as 专题区域,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,区域类型编码 from
			(
				select 企业代码,区域类型编码 from 业务_污染源名录库_总站 as a
				inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
				'+@mlkWhere+' group by 企业代码,行政区代码,区域类型编码
			) as t
			group by 区域类型编码
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,区域类型编码 from
			(
				select 企业代码,区域类型编码 from V_废水统计 as a
				inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
				'+@strWhere+' group by 企业代码,行政区代码,区域类型编码
			) as t
			group by 区域类型编码
		) as b
		on a.区域类型编码=b.区域类型编码
		inner join 系统_代码 as c
		on c.代码类型编码=33 and a.区域类型编码=c.ID
		order by 代码编码'
end
exec(@sql)
end
go

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[StatSolid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[StatSolid]
GO

--=======================================================
-- Author:      hanfei
-- Date:        2011-10-27
-- Description: 固废处理厂录入情况统计
-- 变量@type: 1:按(省/市)统计,2:按省统计,3:按专题区域统计
--=======================================================
CREATE proc [dbo].[StatSolid]
(
	@type int,
	@year varchar(4),
	@quarter varchar(50),
	@regionCode varchar(max),
	@projectArea varchar(max),
	@controlType varchar(1)
)
as
begin
--设置查询条件
declare @strWhere nvarchar(max) --基本信息查询条件
declare @mlkWhere nvarchar(max)--名录库查询条件
set @strWhere = ' where 1=1 '
set @mlkWhere = ' where 1=1 and 企业类型=3 '
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
		set @mlkWhere = @mlkWhere + ' and 年度 = '+ @year
	end
if(@quarter<>'')
	begin
		set @strWhere = @strWhere + ' and 季度 in ('+ @quarter +')'
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @mlkWhere = @mlkWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@type=3 and @projectArea<>'')
	begin
		set @strWhere = @strWhere + ' and 区域类型编码 in ('+@projectArea+')'
	end
if(@controlType<>0)
	begin
		set @mlkWhere =  @mlkWhere + ' and 污染源控制级别代码 = ' + @controlType
	end
	
declare @sql nvarchar(max)
--按(省/市)统计
if @type=1
begin
	set @sql=
		'select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,2)+''0000''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_固废处理厂统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,2)+''0000''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		union all
		select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,4)+''00'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,4)+''00''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,4)+''00'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_固废处理厂统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,4)+''00''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		order by 代码编码'
end
--按省统计
if(@type=2)	
begin
	set @sql=
		'select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,2)+''0000''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_固废处理厂统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,2)+''0000''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		order by 代码编码'
end
--按专题区域统计
if(@type=3)
begin
	set @sql=
		'select c.代码编码 as 专题区域代码,c.代码名称 as 专题区域,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,区域类型编码 from
			(
				select 企业代码,区域类型编码 from 业务_污染源名录库_总站 as a
				inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
				'+@mlkWhere+' group by 企业代码,行政区代码,区域类型编码
			) as t
			group by 区域类型编码
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,区域类型编码 from
			(
				select 企业代码,区域类型编码 from V_固废处理厂统计 as a
				inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
				'+@strWhere+' group by 企业代码,行政区代码,区域类型编码
			) as t
			group by 区域类型编码
		) as b
		on a.区域类型编码=b.区域类型编码
		inner join 系统_代码 as c
		on c.代码类型编码=33 and a.区域类型编码=c.ID
		order by 代码编码'
end
exec(@sql)
end
go

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[StatSewagefarm]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[StatSewagefarm]
GO

--=======================================================
-- Author:      hanfei
-- Date:        2011-10-27
-- Description: 污水处理厂录入情况统计
-- 变量@type: 1:按(省/市)统计,2:按省统计,3:按专题区域统计
--=======================================================
CREATE proc [dbo].[StatSewagefarm]
(
	@type int,
	@year varchar(4),
	@quarter varchar(50),
	@regionCode varchar(max),
	@projectArea varchar(max),
	@controlType varchar(1)
)
as
begin
--设置查询条件
declare @strWhere nvarchar(max) --基本信息查询条件
declare @mlkWhere nvarchar(max)--名录库查询条件
set @strWhere = ' where 1=1 '
set @mlkWhere = ' where 1=1 and 企业类型=2 '
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
		set @mlkWhere = @mlkWhere + ' and 年度 = '+ @year
	end
if(@quarter<>'')
	begin
		set @strWhere = @strWhere + ' and 季度 in ('+ @quarter +')'
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @mlkWhere = @mlkWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@type=3 and @projectArea<>'')
	begin
		set @strWhere = @strWhere + ' and 区域类型编码 in ('+@projectArea+')'
	end
if(@controlType<>0)
	begin
		set @mlkWhere =  @mlkWhere + ' and 污染源控制级别代码 = ' + @controlType
	end
	
declare @sql nvarchar(max)
--按(省/市)统计
if @type=1
begin
	set @sql=
		'select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,2)+''0000''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_污水处理厂统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,2)+''0000''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		union all
		select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,4)+''00'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,4)+''00''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,4)+''00'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_污水处理厂统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,4)+''00''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		order by 代码编码'
end
--按省统计
if(@type=2)	
begin
	set @sql=
		'select c.代码编码 as 行政区代码,c.代码名称 as 行政区,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select 企业代码,行政区代码 from 业务_污染源名录库_总站'+@mlkWhere+'
				group by 企业代码,行政区代码
			) as a
			group by substring(行政区代码,1,2)+''0000''
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,substring(行政区代码,1,2)+''0000'' as 行政区代码 from
			(
				select  企业代码,行政区代码 from V_污水处理厂统计'+@strWhere+'
				group by 企业代码,行政区代码
			) as t
			group by substring(行政区代码,1,2)+''0000''
		) as b
		on a.行政区代码=b.行政区代码
		inner join 系统_代码 as c
		on c.代码类型编码=14 and a.行政区代码=c.代码编码
		order by 代码编码'
end
--按专题区域统计
if(@type=3)
begin
	set @sql=
		'select c.代码编码 as 专题区域代码,c.代码名称 as 专题区域,应录数据量,isnull(实录数据量,0) as 实录数据量,cast(cast(实录数据量 as decimal(18,4))/应录数据量 as decimal(18,4)) as 录入率 from
		(
			select count(企业代码) as 应录数据量,区域类型编码 from
			(
				select 企业代码,区域类型编码 from 业务_污染源名录库_总站 as a
				inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
				'+@mlkWhere+' group by 企业代码,行政区代码,区域类型编码
			) as t
			group by 区域类型编码
		) as a
		left join 
		(
			select count(企业代码) as 实录数据量,区域类型编码 from
			(
				select 企业代码,区域类型编码 from V_污水处理厂统计 as a
				inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
				'+@strWhere+' group by 企业代码,行政区代码,区域类型编码
			) as t
			group by 区域类型编码
		) as b
		on a.区域类型编码=b.区域类型编码
		inner join 系统_代码 as c
		on c.代码类型编码=33 and a.区域类型编码=c.ID
		order by 代码编码'
end
exec(@sql)
end
go

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetGasAmount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetGasAmount]
GO

--========================================================================
-- Author:      hanfei
-- Date:        2011-10-25
-- Description: 废气排放量统计
-- 废气排放量包括：行业、地区(省/市)、地区(省)、专题区域4类的统计
-- 变量@type: 1:行业,2:流域,3:水系,4:地区(省/市),5:地区(省),6:专题区域
-- 变量@statType：1:国控污染源企业,2:录入污染源企业
--========================================================================
CREATE procedure [dbo].[GetGasAmount]
	@type int ,--计算标准
	@year varchar(4),--年
	@quarter varchar(20),--季度
	@regionCode varchar(max),--地区编码
	@tradeCode varchar(max),--行业代码
	@whaterCode varchar(max),--受纳水体代码
	@polutionCode varchar(max),--监测项目
	@controlType varchar(50),--废水控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int,	--统计范围
	@projectAreaCode varchar(50) --专题区域编码
as
begin

if object_id('tempdb..##tmp_FQPF') is not null begin drop table ##tmp_FQPF end

--设置查询条件
declare @strWhere nvarchar(max)
declare @accountView nvarchar(50)
set @strWhere=' where 1=1 '
set @accountView = ''
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@quarter<>'')
	begin
		set @strWhere = @strWhere + ' and 季度 in ('+ @quarter +')'
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in('+@tradeCode+')'
	end
if(@polutionCode<>'')
    begin
		set @strWhere = @strWhere + 'and 监测项目编号 in ('+@polutionCode+')'
    end
if(@controlType<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码 in ('+@controlType+')'
    end
if(@controlTypeGas<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码气 in ('+@controlTypeGas+')'
    end
if(@statType=1)
	begin
		set @accountView='V_废气排放量计算_名录库'
	end
if(@statType=2)
	begin
		set @accountView='V_废气排放量计算'
	end
declare @projectAreaWhere nvarchar(50)
set @projectAreaWhere=''
if(@type=6 and @projectAreaCode<>'')
begin
	set @projectAreaWhere = ' where 代码编码 in ('+@projectAreaCode+')'
end

declare @sql nvarchar(max)
set @sql = 
	   'select 行业代码,行政区代码,企业代码,count(排污设备代码) as 排放设备数量,sum(排放量) as 排放量 
		into ##tmp_FQPF from
		(
			select 行业代码,行政区代码,企业代码,排污设备代码,sum(排放量) as 排放量 from
			(
				select 行业代码,行政区代码,企业代码,排污设备代码,avg(排放量) as 排放量,监测项目编号,季度 from
				(
					select 行业代码,行政区代码,企业代码,排污设备代码,污染物浓度*废气排放量/工况负荷*生产总小时数*平均工况负荷/1000000 as 排放量,监测项目编号,季度
					from '+@accountView+@strWhere+'
				) as t
				group by 行业代码,行政区代码,企业代码,排污设备代码,监测项目编号,季度
			) as t 
			group by 行业代码,行政区代码,企业代码,排污设备代码
		)b group by 行业代码,行政区代码,企业代码'
exec(@sql)
--按行业查询
if(@type=1)
begin
	select '0' as 代码编码,'全部' as 代码名称,count(企业代码) as 企业数量,sum(排放设备数量) as 排放设备数量,sum(排放量) as 排放量
	from ##tmp_FQPF
	union all
	select 代码编码,代码名称,count(企业代码) as 企业数量,sum(排放设备数量) as 排放设备数量,sum(排放量) as 排放量
	from
	(
		select b.代码编码,b.代码名称,a.企业代码,a.排放设备数量,a.排放量
		from ##tmp_FQPF as a inner join 系统_代码 as b
		on 代码类型编码=15 and substring(a.行业代码,1,2)=b.代码编码
	) as t
	group by 代码编码,代码名称
	order by 代码编码
end
--按地区(省/市)查询
if(@type=4)
begin
	select 代码编码,代码名称,企业数量,排放设备数量,排放量
	from 
	(
		select '0' as 代码编码,'全部' as 代码名称,count(企业代码) as 企业数量,sum(排放设备数量) as 排放设备数量,sum(排放量) as 排放量
		from ##tmp_FQPF
		union all
		select 代码编码,代码名称,count(企业代码) as 企业数量,sum(排放设备数量) as 排放设备数量,sum(排放量) as 排放量
		from
		(
			select b.代码编码,b.代码名称,a.企业代码,a.排放设备数量,a.排放量
			from ##tmp_FQPF as a inner join 系统_代码 as b
			on 代码类型编码=14 and substring(a.行政区代码,1,2)+'0000'=b.代码编码
		) as t
		group by 代码编码,代码名称
		union all
		select 代码编码,代码名称,count(企业代码) as 企业数量,sum(排放设备数量) as 排放设备数量,sum(排放量) as 排放量
		from
		(
			select b.代码编码,b.代码名称,a.企业代码,a.排放设备数量,a.排放量
			from ##tmp_FQPF as a inner join 系统_代码 as b
			on 代码类型编码=14 and substring(a.行政区代码,1,4)+'00'=b.代码编码
		) as t
		group by 代码编码,代码名称
	) as t
	order by 代码编码
end
--按地区(省)查询
if(@type=5)
begin
	select '0' as 代码编码,'全部' as 代码名称,count(企业代码) as 企业数量,sum(排放设备数量) as 排放设备数量,sum(排放量) as 排放量
	from ##tmp_FQPF
	union all
	select 代码编码,代码名称,count(企业代码) as 企业数量,sum(排放设备数量) as 排放设备数量,sum(排放量) as 排放量
	from
	(
		select b.代码编码,b.代码名称,a.企业代码,a.排放设备数量,a.排放量
		from ##tmp_FQPF as a inner join 系统_代码 as b
		on 代码类型编码=14 and substring(a.行政区代码,1,2)+'0000'=b.代码编码
	) as t
	group by 代码编码,代码名称
	order by 代码编码
end
--按专题区域查询
if(@type=6)
begin
	set @sql=
	'select 代码编码,代码名称,企业数量,排放设备数量,排放量 from 
	(
		select ''0'' as 代码编码,''全部'' as 代码名称,count(企业代码) as 企业数量,sum(排放设备数量) as 排放设备数量,sum(排放量) as 排放量
		from ##tmp_FQPF where 行政区代码 in (select 行政区编码 from 系统_专题区域代码)
		union all
		select 代码编码,代码名称,count(企业代码) as 企业数量,sum(排放设备数量) as 排放设备数量,sum(排放量) as 排放量
		from
		(
			select c.ID as 代码编码,c.代码名称,a.企业代码,a.排放设备数量,a.排放量
			from ##tmp_FQPF as a 
			inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
			inner join 系统_代码 as c on 代码类型编码=33 and b.区域类型编码=c.ID
		) as t'+@projectAreaWhere+'
		group by 代码编码,代码名称
	) as t where 企业数量>0 and 排放设备数量>0 and 排放量>0
	order by 代码编码'
	exec (@sql)
end

--删除临时表
if object_id('tempdb..##tmp_FQPF') is not null begin drop table ##tmp_FQPF end

end


GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetGasAmountCityList]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetGasAmountCityList]
GO

--=========================================================================
-- Author:      hanfei
-- Date:        2011-10-25
-- Description: 废气排放量统计企业统计
-- 废气的排放量包括：行业、地区(省/市)、地区(省)、专题区域4类的统计
-- 变量@type: 1:行业,2:流域,3:水系,4:地区(省/市),5:地区(省),6:专题区域
-- 变量@statType：1:国控污染源企业,2:录入污染源企业
--=========================================================================
CREATE procedure [dbo].[GetGasAmountCityList]
	@type int,					--计算方法
	@priparm varchar(50),		--代码编码
	@year varchar(4),			--监测年
	@quarter varchar(20),		--季度
	@regionCode varchar(max),	--行政区编码
	@tradeCode varchar(max),	--行业编码
	@whaterCode varchar(max),	--受纳水体编码
	@polutionCode varchar(max),	--监测项目编码
	@controlType varchar(50),	--废气控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int	--统计范围
as
begin
--设置查询条件
declare @strWhere nvarchar(max)
declare @accountView nvarchar(50)
set @strWhere=' where 1=1 '
set @accountView = ''
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@quarter<>'')
	begin
		set @strWhere = @strWhere + ' and 季度 in ('+ @quarter +')'
	end
if(@polutionCode<>'')
    begin
		set @strWhere = @strWhere + 'and 监测项目编号 in ('+@polutionCode+')'
    end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in('+@tradeCode+')'
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@controlType<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码 in ('+@controlType+')'
    end
if(@controlTypeGas<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码气 in ('+@controlTypeGas+')'
    end
if(@statType=1)
	begin
		set @accountView='V_废气排放量计算_名录库'
	end
if(@statType=2)
	begin
		set @accountView='V_废气排放量计算'
	end

if(@priparm<>'0')
begin
	--行业
	if(@type=1)
	begin
		set @strWhere = @strWhere + ' and substring(行业代码,1,2) = '''+@priparm+''''
	end
	--地区省/市
	if(@type=4 or @type=5)
	begin
		if(substring(@priparm,3,4)='0000')
		begin
			set @strWhere = @strWhere + ' and substring(行政区代码,1,2)+''0000'' = ''' + @priparm+''''
		end
		else
		begin
			set @strWhere = @strWhere + ' and substring(行政区代码,1,4)+''00'' = '''+@priparm+''''
		end
	end
	if(@type=6)
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in (select 行政区编码 from 系统_专题区域代码 where 区域类型编码='''+@priparm+''')'
	end
end
else
begin
	if(@type=6)
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in (select 行政区编码 from 系统_专题区域代码)'
	end
end

declare @sql nvarchar(max)
set @sql = 
	   'select 行政区代码,企业代码,详细名称,count(排污设备代码) as 排放设备数量,sum(排放量) as 排放量 
		from
		(
			select 行政区代码,企业代码,详细名称,排污设备代码,sum(排放量) as 排放量 from
			(
				select 行政区代码,企业代码,详细名称,排污设备代码,avg(排放量) as 排放量,监测项目编号,季度 from
				(
					select 行政区代码,企业代码,详细名称,排污设备代码,污染物浓度*废气排放量/工况负荷*生产总小时数*平均工况负荷/1000000 as 排放量,监测项目编号,季度
					from '+@accountView+@strWhere+'
				) as t
				group by 行政区代码,企业代码,详细名称,排污设备代码,监测项目编号,季度
			) as t 
			group by 行政区代码,企业代码,详细名称,排污设备代码
		)b group by 行政区代码,企业代码,详细名称
		order by 行政区代码,企业代码'
exec(@sql)
end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetWaterAmount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetWaterAmount]
GO

--========================================================================
-- Author:      hanfei
-- Date:        2011-10-25
-- Description: 废水排放量统计
-- 废水排放量包括：行业、流域、水系、地区(省/市)、地区(省)、专题区域6类的统计
-- 变量@type: 1:行业,2:流域,3:水系,4:地区(省/市),5:地区(省),6:专题区域
-- 变量@statType：1:国控污染源企业,2:录入污染源企业
--========================================================================
CREATE procedure [dbo].[GetWaterAmount]
	@type int ,--计算标准
	@year varchar(4),--年
	@quarter varchar(20),--季度
	@regionCode varchar(max),--地区编码
	@tradeCode varchar(max),--行业代码
	@whaterCode varchar(max),--受纳水体代码
	@polutionCode varchar(max),--监测项目
	@controlType varchar(50),--废水控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int,	--统计范围
	@projectAreaCode varchar(50) --专题区域编码
as
begin

if object_id('tempdb..##tmp_FSPF') is not null begin drop table ##tmp_FSPF end

--设置查询条件
declare @strWhere nvarchar(max)
declare @accountView nvarchar(50)
set @strWhere=' where 1=1 '
set @accountView = ''
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@quarter<>'')
	begin
		set @strWhere = @strWhere + ' and 季度 in ('+ @quarter +')'
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in('+@tradeCode+')'
	end
if(@whaterCode<>'')
	 begin
		set @strWhere = @strWhere + ' and 受纳水体代码 in('+@whaterCode+')'
	 end
if(@polutionCode<>'')
    begin
		set @strWhere = @strWhere + 'and 监测项目编号 in ('+@polutionCode+')'
    end
if(@controlType<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码 in ('+@controlType+')'
    end
if(@controlTypeGas<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码气 in ('+@controlTypeGas+')'
    end
if(@statType=1)
	begin
		set @accountView='V_废水排放量计算_名录库'
	end
if(@statType=2)
	begin
		set @accountView='V_废水排放量计算'
	end
declare @projectAreaWhere nvarchar(50)
set @projectAreaWhere=''
if(@type=6 and @projectAreaCode<>'')
begin
	set @projectAreaWhere = ' where 代码编码 in ('+@projectAreaCode+')'
end

declare @sql nvarchar(max)
set @sql = 
	   'select 企业代码,count(监测点代码) as 监测点数量,行政区代码,受纳水体代码,行业代码,sum(排放量) as 排放量
		into ##tmp_FSPF from 
		(
			select 企业代码,监测点代码,行政区代码,受纳水体代码,行业代码,sum(排放量) as 排放量 from 
			(
				select 企业代码,监测点代码,监测项目编号,季度,行政区代码,受纳水体代码,行业代码,avg(单次排放量) as 排放量 from 
				(
					select 企业代码,监测点代码,监测项目编号,季度,行政区代码,受纳水体代码,行业代码,污染物浓度*废水排放量*生产总天数/工况负荷*平均工况负荷/1000 as 单次排放量
					from '+@accountView+@strWhere+'
				) as t
				group by 企业代码,监测点代码,监测项目编号,季度,行政区代码,受纳水体代码,行业代码
			) as t
			group by 企业代码,监测点代码,行政区代码,受纳水体代码,行业代码
		) as t 
		group by 企业代码,行政区代码,受纳水体代码,行业代码'
exec(@sql)
--按行业查询
if(@type=1)
begin
	select '0' as 代码编码,'全部' as 代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
	from 
	(
		select 企业代码,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量 from ##tmp_FSPF
		group by 企业代码
	) as t
	union all
	select 代码编码,代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
	from
	(
		select b.代码编码,b.代码名称,a.企业代码,sum(a.监测点数量) as 监测点数量,sum(a.排放量) as 排放量
		from ##tmp_FSPF as a inner join 系统_代码 as b
		on 代码类型编码=15 and substring(a.行业代码,1,2)=b.代码编码
		group by b.代码编码,b.代码名称,a.企业代码
	) as t
	group by 代码编码,代码名称
	order by 代码编码
end
--按流域查询
if(@type=2)
begin
	select '0' as 代码编码,'全部' as 代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
	from 
	(
		select 企业代码,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量 from ##tmp_FSPF
		group by 企业代码
	) as t
	union all
	select 代码编码,代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
	from
	(
		select b.代码编码,b.代码名称,a.企业代码,sum(a.监测点数量) as 监测点数量,sum(a.排放量) as 排放量
		from ##tmp_FSPF as a inner join 系统_代码 as b
		on 代码类型编码=20 and substring(a.受纳水体代码,1,1)=b.代码编码
		group by b.代码编码,b.代码名称,a.企业代码
	) as t
	group by 代码编码,代码名称
	order by 代码编码
end
--按水系查询
if(@type=3)
begin
	select 代码编码,代码名称,企业数量,监测点数量,排放量
	from
	(
		select '0' as 代码编码,'全部' as 代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
		from 
		(
			select 企业代码,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量 from ##tmp_FSPF
			group by 企业代码
		) as t
		union all
		select 代码编码,代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
		from
		(
			select b.代码编码,b.代码名称,a.企业代码,sum(a.监测点数量) as 监测点数量,sum(a.排放量) as 排放量
			from ##tmp_FSPF as a inner join 系统_代码 as b
			on 代码类型编码=20 and substring(a.受纳水体代码,1,1)=b.代码编码
			group by b.代码编码,b.代码名称,a.企业代码
		) as t
		group by 代码编码,代码名称
		union all
		select 代码编码,代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
		from
		(
			select b.代码编码,b.代码名称,a.企业代码,sum(a.监测点数量) as 监测点数量,sum(a.排放量) as 排放量
			from ##tmp_FSPF as a inner join 系统_代码 as b
			on b.代码类型编码=16 and substring(a.受纳水体代码,1,2)+'00000000'=b.代码编码
			group by b.代码编码,b.代码名称,a.企业代码
		) as t
		group by 代码编码,代码名称
	) as t 
	order by 代码编码
end
--按地区(省/市)查询
if(@type=4)
begin
	select 代码编码,代码名称,企业数量,监测点数量,排放量
	from
	(
		select '0' as 代码编码,'全部' as 代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
		from 
		(
			select 企业代码,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量 from ##tmp_FSPF
			group by 企业代码
		) as t
		union all
		select 代码编码,代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
		from
		(
			select b.代码编码,b.代码名称,a.企业代码,sum(a.监测点数量) as 监测点数量,sum(a.排放量) as 排放量
			from ##tmp_FSPF as a inner join 系统_代码 as b
			on 代码类型编码=14 and substring(a.行政区代码,1,2)+'0000'=b.代码编码
			group by b.代码编码,b.代码名称,a.企业代码
		) as t
		group by 代码编码,代码名称
		union all
		select 代码编码,代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
		from
		(
			select b.代码编码,b.代码名称,a.企业代码,sum(a.监测点数量) as 监测点数量,sum(a.排放量) as 排放量
			from ##tmp_FSPF as a inner join 系统_代码 as b
			on 代码类型编码=14 and substring(a.行政区代码,1,4)+'00'=b.代码编码
			group by b.代码编码,b.代码名称,a.企业代码
		) as t
		group by 代码编码,代码名称
	) as t
	order by 代码编码
end
--按地区(省)查询
if(@type=5)
begin
	select '0' as 代码编码,'全部' as 代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
	from 
	(
		select 企业代码,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量 from ##tmp_FSPF
		group by 企业代码
	) as t
	union all
	select 代码编码,代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
	from
	(
		select b.代码编码,b.代码名称,a.企业代码,sum(a.监测点数量) as 监测点数量,sum(a.排放量) as 排放量
		from ##tmp_FSPF as a inner join 系统_代码 as b
		on 代码类型编码=14 and substring(a.行政区代码,1,2)+'0000'=b.代码编码
		group by b.代码编码,b.代码名称,a.企业代码
	) as t
	group by 代码编码,代码名称
	order by 代码编码
end
--按专题区域查询
if(@type=6)
begin
	set @sql=
	'select 代码编码,代码名称,企业数量,监测点数量,排放量 from 
	(
		select ''0'' as 代码编码,''全部'' as 代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
		from 
		(
			select 企业代码,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量 from ##tmp_FSPF
			where 行政区代码 in (select 行政区编码 from 系统_专题区域代码)
			group by 企业代码
		) as t
		union all
		select 代码编码,代码名称,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(排放量) as 排放量
		from
		(
			select c.ID as 代码编码,c.代码名称,a.企业代码,sum(a.监测点数量) as 监测点数量,sum(a.排放量) as 排放量
			from ##tmp_FSPF as a 
			inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
			inner join 系统_代码 as c on 代码类型编码=33 and b.区域类型编码=c.ID
			group by c.ID,c.代码名称,a.企业代码
		) as t'+@projectAreaWhere+'
		group by 代码编码,代码名称
	) as t where 企业数量>0 and 监测点数量>0 and 排放量>0
	order by 代码编码'
	exec (@sql)
end

--删除临时表
--if object_id('tempdb..##tmp_FSPF') is not null begin drop table ##tmp_FSPF end

end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetWaterAmountCityList]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetWaterAmountCityList]
GO

--========================================================================
-- Author:      hanfei
-- Date:        2011-10-25
-- Description: 废水排放量统计企业统计
-- 废水的排放量包括：行业、流域、水系、地区(省/市)、地区(省)、专题区域6类的统计
-- 变量@type: 1:行业,2:流域,3:水系,4:地区(省/市),5:地区(省),6:专题区域
-- 变量@statType：1:国控污染源企业,2:录入污染源企业
--========================================================================
CREATE PROCEDURE [dbo].[GetWaterAmountCityList]
	@type int,					--计算方法
	@priparm varchar(50),		--代码编码
	@year varchar(4),			--监测年
	@quarter varchar(20),		--季度
	@regionCode varchar(max),	--行政区编码
	@tradeCode varchar(max),	--行业编码
	@whaterCode varchar(max),	--受纳水体编码
	@polutionCode varchar(max),	--监测项目编码
	@controlType varchar(50),	--废气控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int	--统计范围
as
begin
--设置查询条件
declare @strWhere nvarchar(max)
declare @accountView nvarchar(50)
set @strWhere=' where 1=1 '
set @accountView = ''
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@quarter<>'')
	begin
		set @strWhere = @strWhere + ' and 季度 in ('+ @quarter +')'
	end
if(@polutionCode<>'')
    begin
		set @strWhere = @strWhere + 'and 监测项目编号 in ('+@polutionCode+')'
    end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in('+@tradeCode+')'
	end
if(@whaterCode<>'')
	 begin
		set @strWhere = @strWhere + ' and 受纳水体代码 in('+@whaterCode+')'
	 end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@controlType<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码 in ('+@controlType+')'
    end
if(@controlTypeGas<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码气 in ('+@controlTypeGas+')'
    end	
if(@statType=1)
	begin
		set @accountView='V_废水排放量计算_名录库'
	end
if(@statType=2)
	begin
		set @accountView='V_废水排放量计算'
	end
    
if(@priparm<>'0')
begin
	--行业
	if(@type=1)
	begin
		set @strWhere = @strWhere + ' and substring(行业代码,1,2) = '''+@priparm+''''
	end
	--流域/水系
	if(@type=2 or @type=3)
	begin
		if(len(@priparm)=1)
		begin
			set @strWhere = @strWhere + ' and substring(受纳水体代码,1,1) = ''' + @priparm+''''
		end
		else
		begin
			set @strWhere = @strWhere + ' and substring(受纳水体代码,1,2)+''00000000'' = '''+@priparm+''''
		end
	end
	--地区省/市
	if(@type=4 or @type=5)
	begin
		if(substring(@priparm,3,4)='0000')
		begin
			set @strWhere = @strWhere + ' and substring(行政区代码,1,2)+''0000'' = ''' + @priparm+''''
		end
		else
		begin
			set @strWhere = @strWhere + ' and substring(行政区代码,1,4)+''00'' = '''+@priparm+''''
		end
	end
	if(@type=6)
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in (select 行政区编码 from 系统_专题区域代码 where 区域类型编码='''+@priparm+''')'
	end
end
else
begin
	if(@type=6)
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in (select 行政区编码 from 系统_专题区域代码)'
	end
end

declare @sql nvarchar(max)
set @sql = 
	   'select 企业代码,详细名称,count(监测点代码) as 监测点数量,行政区代码,sum(排放量) as 排放量
		from 
		(
			select 企业代码,详细名称,监测点代码,行政区代码,受纳水体代码,sum(排放量) as 排放量 from 
			(
				select 企业代码,详细名称,监测点代码,监测项目编号,季度,行政区代码,受纳水体代码,avg(单次排放量) as 排放量 from 
				(
					select 企业代码,详细名称,监测点代码,监测项目编号,季度,行政区代码,受纳水体代码,污染物浓度*废水排放量*生产总天数/工况负荷*平均工况负荷/1000 as 单次排放量
					from '+@accountView+@strWhere+'
				) as t
				group by 企业代码,详细名称,监测点代码,监测项目编号,季度,行政区代码,受纳水体代码
			) as t
			group by 企业代码,详细名称,监测点代码,行政区代码,受纳水体代码
		) as t 
		group by 企业代码,详细名称,行政区代码
		order by 企业代码'
exec(@sql)
end
go

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetWSAmount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetWSAmount]
GO

--========================================================================
-- Author:      hanfei
-- Date:        2011-10-25
-- Description: 污水处理厂排放量统计
-- 污水处理厂的排放量包括：流域、水系、地区(省/市)、地区(省)、专题区域5类的统计
-- 变量@type: 1:行业,2:流域,3:水系,4:地区(省/市),5:地区(省),6:专题区域
-- 变量@statType：1:国控污染源企业,2:录入污染源企业
--========================================================================
CREATE procedure [dbo].[GetWSAmount]
	@type int ,--计算标准
	@year varchar(4),--年
	@quarter varchar(20),--季度
	@regionCode varchar(max),--地区编码
	@tradeCode varchar(max),--行业代码
	@whaterCode varchar(max),--受纳水体代码
	@polutionCode varchar(max),--监测项目
	@controlType varchar(50),--废水控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int,	--统计范围
	@projectAreaCode varchar(50) --专题区域编码
as
begin
if object_id('tempdb..##tmp_WSPFL') is not null begin drop table ##tmp_WSPFL end
--设置查询条件
declare @strWhere nvarchar(max)
declare @accountView nvarchar(50)
set @strWhere=' where 1=1 '
set @accountView = ''
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@quarter<>'')
	begin
		set @strWhere = @strWhere + ' and 季度 in ('+ @quarter +')'
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in('+@tradeCode+')'
	end
if(@whaterCode<>'')
	 begin
		set @strWhere = @strWhere + ' and 受纳水体代码 in('+@whaterCode+')'
	 end
if(@polutionCode<>'')
    begin
		set @strWhere = @strWhere + 'and 监测项目编号 in ('+@polutionCode+')'
    end
if(@statType=1)
	begin
		set @accountView='V_污水处理厂排放量计算_名录库'
	end
if(@statType=2)
	begin
		set @accountView='V_污水处理厂排放量计算'
	end
declare @projectAreaWhere nvarchar(50)
set @projectAreaWhere=''
if(@type=6 and @projectAreaCode<>'')
begin
	set @projectAreaWhere = ' where 代码编码 in ('+@projectAreaCode+')'
end

declare @sql nvarchar(max)
--按流域查询
if(@type=2)
begin
	set @sql=
		   'select 受纳水体代码,count(企业代码) as 企业数量,sum(污染物排放量) as 污染物排放量
			into ##tmp_WSPFL from (
				select 受纳水体代码,企业代码,sum(污染物排放量) as 污染物排放量 from
				(
					select substring(受纳水体代码,1,1) as 受纳水体代码,企业代码,监测项目编号,季度,avg(污染物排放量) as 污染物排放量
					from '+@accountView+@strWhere+'
					group by 受纳水体代码,企业代码,监测项目编号,季度
				) as t 
				group by 受纳水体代码,企业代码
			) as t
			group by 受纳水体代码'
	exec(@sql)
	select '0' as 代码编码,'全部' as 代码名称,sum(企业数量) as 企业数量,cast(sum(污染物排放量) as decimal(18,6)) as 排放量 from 
	(
		select b.代码编码,b.代码名称,a.企业数量,a.污染物排放量 
		from ##tmp_WSPFL as a inner join 系统_代码 as b
		on 代码类型编码=20 and a.受纳水体代码=b.代码编码
	) as t
	union all
	select b.代码编码,b.代码名称,a.企业数量,cast(污染物排放量 as decimal(18,6)) as 排放量
	from ##tmp_WSPFL as a inner join 系统_代码 as b
	on 代码类型编码=20 and a.受纳水体代码=b.代码编码
	order by 代码编码
end
--按水系查询
if(@type=3)
begin
	set @sql=
		'select 受纳水体代码,企业代码,污染物排放量 into ##tmp_WSPFL from (
			select 受纳水体代码,企业代码,sum(污染物排放量) as 污染物排放量 from
			(
				select 受纳水体代码,企业代码,监测项目编号,季度,avg(污染物排放量) as 污染物排放量
				from '+@accountView+@strWhere+'
				group by 受纳水体代码,企业代码,监测项目编号,季度
			) as t 
			group by 受纳水体代码,企业代码
		) as t'
	exec(@sql)
	select 代码编码,代码名称,企业数量,排放量 from (
		select '0' as 代码编码,'全部' as 代码名称,sum(企业数量) as 企业数量,cast(sum(污染物排放量) as decimal(18,6)) as 排放量 from 
		(
			select 代码编码,代码名称,count(企业代码) as 企业数量,sum(污染物排放量) as 污染物排放量
			from 
			(
				select b.代码编码,b.代码名称,企业代码,污染物排放量 from ##tmp_WSPFL as a 
				inner join 系统_代码 as b
				on b.代码类型编码=20 and substring(a.受纳水体代码,1,1)=b.代码编码
			) as t
			group by 代码编码,代码名称
		) as t
		union all
		select 代码编码,代码名称,count(企业代码) as 企业数量,cast(sum(污染物排放量) as decimal(18,6)) as 排放量
		from 
		(
			select b.代码编码,b.代码名称,企业代码,污染物排放量 from ##tmp_WSPFL as a 
			inner join 系统_代码 as b
			on b.代码类型编码=20 and substring(a.受纳水体代码,1,1)=b.代码编码
		) as t
		group by 代码编码,代码名称
		union all
		select 代码编码,代码名称,count(企业代码) as 企业数量,cast(sum(污染物排放量) as decimal(18,6)) as 排放量
		from 
		(
			select b.代码编码,b.代码名称,企业代码,污染物排放量 from ##tmp_WSPFL as a 
			inner join 系统_代码 as b
			on b.代码类型编码=16 and substring(a.受纳水体代码,1,2)+'00000000'=b.代码编码
		) as t
		group by 代码编码,代码名称
	) as t
	order by 代码编码
end
--按地区(省/市)查询
if(@type=4)
begin
	set @sql = 
		   'select 行政区代码,企业代码,污染物排放量
			into ##tmp_WSPFL from (
				select 行政区代码,企业代码,sum(污染物排放量) as 污染物排放量 from
				(
					select 行政区代码,企业代码,监测项目编号,季度,avg(污染物排放量) as 污染物排放量
					from '+@accountView+@strWhere+'
					group by 行政区代码,企业代码,监测项目编号,季度
				) as t 
				group by 行政区代码,企业代码
			) as t'
	exec(@sql)
	select '0' as 代码编码,'全部' as 代码名称,count(企业代码) as 企业数量,cast(sum(污染物排放量) as decimal(18,6)) as 排放量 from 
	(
		select b.代码编码,b.代码名称,a.企业代码,a.污染物排放量 
		from ##tmp_WSPFL as a inner join 系统_代码 as b
		on 代码类型编码=14 and a.行政区代码=b.代码编码
	) as t
	union all
	select 代码编码,代码名称,count(企业代码) as 企业数量,cast(sum(污染物排放量) as decimal(18,6)) as 排放量 from
	(
		select b.代码编码,b.代码名称,a.企业代码,a.污染物排放量
		from ##tmp_WSPFL as a inner join 系统_代码 as b
		on 代码类型编码=14 and substring(a.行政区代码,1,2)+'0000'=b.代码编码
	) as t
	group by 代码编码,代码名称
	union all
	select 代码编码,代码名称,count(企业代码) as 企业数量,cast(sum(污染物排放量) as decimal(18,6)) as 排放量 from
	(
		select b.代码编码,b.代码名称,a.企业代码,a.污染物排放量
		from ##tmp_WSPFL as a inner join 系统_代码 as b
		on 代码类型编码=14 and substring(a.行政区代码,1,4)+'00'=b.代码编码
	) as t
	group by 代码编码,代码名称
	order by 代码编码
end
--按地区(省)查询
if(@type=5)
begin
	set @sql =
		   'select 行政区代码,count(企业代码) as 企业数量,sum(污染物排放量) as 污染物排放量
			into ##tmp_WSPFL from (
				select 行政区代码,企业代码,sum(污染物排放量) as 污染物排放量 from
				(
					select substring(行政区代码,1,2)+''0000'' as 行政区代码,企业代码,监测项目编号,季度,avg(污染物排放量) as 污染物排放量
					from '+@accountView+@strWhere+'
					group by 行政区代码,企业代码,监测项目编号,季度
				) as t 
				group by 行政区代码,企业代码
			) as t
			group by 行政区代码'
	exec(@sql)
	select '0' as 代码编码,'全部' as 代码名称,sum(企业数量) as 企业数量,cast(sum(污染物排放量) as decimal(18,6)) as 排放量 from 
	(
		select b.代码编码,b.代码名称,a.企业数量,a.污染物排放量 
		from ##tmp_WSPFL as a inner join 系统_代码 as b
		on 代码类型编码=14 and a.行政区代码=b.代码编码
	) as t
	union all
	select b.代码编码,b.代码名称,a.企业数量,cast(污染物排放量 as decimal(18,6)) as 排放量
	from ##tmp_WSPFL as a inner join 系统_代码 as b
	on 代码类型编码=14 and a.行政区代码=b.代码编码
	order by 代码编码
end
--按专题区域查询
if(@type=6)
begin
	set @sql = 
		   'select 行政区代码,企业代码,污染物排放量
			into ##tmp_WSPFL from (
				select 行政区代码,企业代码,sum(污染物排放量) as 污染物排放量 from
				(
					select 行政区代码,企业代码,监测项目编号,季度,avg(污染物排放量) as 污染物排放量
					from '+@accountView+@strWhere+'
					group by 行政区代码,企业代码,监测项目编号,季度
				) as t 
				group by 行政区代码,企业代码
			) as t'
	exec(@sql)
	set @sql=
	'select 代码编码,代码名称,企业数量,排放量 from 
	(
		select ''0'' as 代码编码,''全部'' as 代码名称,count(企业代码) as 企业数量,cast(sum(污染物排放量) as decimal(18,6)) as 排放量 from 
		(
			select 企业代码,污染物排放量 from ##tmp_WSPFL where 行政区代码 in (select 行政区编码 from 系统_专题区域代码)
		) as t
		union all
		select 代码编码,代码名称,count(企业代码) as 企业数量,cast(sum(污染物排放量) as decimal(18,6)) as 排放量
		from (
			select c.id as 代码编码,c.代码名称,a.企业代码,污染物排放量
			from ##tmp_WSPFL as a
			inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
			inner join 系统_代码 as c on 代码类型编码=33 and b.区域类型编码=c.ID
		) as t '+@projectAreaWhere+'
		group by 代码编码,代码名称
	) as t where 企业数量>0 and 排放量>0
	order by 代码编码'
	exec(@sql)
end

--删除临时表
if object_id('tempdb..##tmp_WSPFL') is not null begin drop table ##tmp_WSPFL end

end


GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetWSAmountCityList]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetWSAmountCityList]
GO

--=========================================================================
-- Author:      hanfei
-- Date:        2011-10-25
-- Description: 污水处理厂排放量统计企业统计
-- 污水处理厂的排放量包括：流域、水系、地区(省/市)、地区(省)、专题区域5类的统计
-- 变量@type: 1:行业,2:流域,3:水系,4:地区(省/市),5:地区(省),6:专题区域
-- 变量@statType：1:国控污染源企业,2:录入污染源企业
--=========================================================================
CREATE procedure [dbo].[GetWSAmountCityList]
	@type int,					--计算方法
	@priparm varchar(50),		--代码编码
	@year varchar(4),			--监测年
	@quarter varchar(20),		--季度
	@regionCode varchar(max),	--行政区编码
	@tradeCode varchar(max),	--行业编码
	@whaterCode varchar(max),	--受纳水体编码
	@polutionCode varchar(max),	--监测项目编码
	@controlType varchar(50),	--废气控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int	--统计范围
as
begin
--设置查询条件
declare @strWhere nvarchar(max)
declare @accountView nvarchar(50)
set @strWhere=' where 1=1 '
set @accountView = ''
if(@year<>'')
	begin
		set @strWhere = @strWhere + ' and 监测年 = '+ @year
	end
if(@quarter<>'')
	begin
		set @strWhere = @strWhere + ' and 季度 in ('+ @quarter +')'
	end
if(@polutionCode<>'')
    begin
		set @strWhere = @strWhere + 'and 监测项目编号 in ('+@polutionCode+')'
    end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in('+@tradeCode+')'
	end
if(@whaterCode<>'')
	 begin
		set @strWhere = @strWhere + ' and 受纳水体代码 in('+@whaterCode+')'
	 end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end
if(@statType=1)
	begin
		set @accountView='V_污水处理厂排放量计算_名录库'
	end
if(@statType=2)
	begin
		set @accountView='V_污水处理厂排放量计算'
	end
	
if(@priparm<>'0')
begin
	--流域/水系
	if(@type=2 or @type=3)
	begin
		if(len(@priparm)=1)
		begin
			set @strWhere = @strWhere + ' and substring(受纳水体代码,1,1) = ''' + @priparm+''''
		end
		else
		begin
			set @strWhere = @strWhere + ' and substring(受纳水体代码,1,2)+''00000000'' = '''+@priparm+''''
		end
	end
	--地区省/市
	if(@type=4 or @type=5)
	begin
		if(substring(@priparm,3,4)='0000')
		begin
			set @strWhere = @strWhere + ' and substring(行政区代码,1,2)+''0000'' = ''' + @priparm+''''
		end
		else
		begin
			set @strWhere = @strWhere + ' and substring(行政区代码,1,4)+''00'' = '''+@priparm+''''
		end
	end
	if(@type=6)
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in (select 行政区编码 from 系统_专题区域代码 where 区域类型编码='''+@priparm+''')'
	end
end
else
begin
	if(@type=6)
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in (select 行政区编码 from 系统_专题区域代码)'
	end
end

declare @sql nvarchar(max)
set @sql = 
	   'select 企业代码,详细名称,行政区代码,cast(sum(污染物排放量) as decimal(18,6)) as 排放量 from
		(
			select 企业代码,详细名称,行政区代码,监测项目编号,季度,avg(污染物排放量) as 污染物排放量
			from '+@accountView+@strWhere+'
			group by 企业代码,详细名称,行政区代码,监测项目编号,季度
		) as t 
		group by 企业代码,详细名称,行政区代码
		order by 企业代码'

exec(@sql)
end

GO


--为数据查询创建临时表
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_废气生产情况模板')
	begin 
		drop table temp_废气生产情况模板
	end
	select top 0 * into temp_废气生产情况模板 from 废气生产情况模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_废水生产情况模板')
	begin 
		drop table temp_废水生产情况模板
	end
	select top 0 * into temp_废水生产情况模板 from 废水生产情况模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_工业废气基本模板')
	begin 
		drop table temp_工业废气基本模板
	end
	select top 0 * into temp_工业废气基本模板 from 工业废气基本模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_工业废水基本模板')
	begin 
		drop table temp_工业废水基本模板
	end
	select top 0 * into temp_工业废水基本模板 from 工业废水基本模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_固废废气基本模板')
	begin 
		drop table temp_固废废气基本模板
	end
	select top 0 * into temp_固废废气基本模板 from 固废废气基本模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_固废废水基本模板')
	begin 
		drop table temp_固废废水基本模板
	end
	select top 0 * into temp_固废废水基本模板 from 固废废水基本模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_污水处理厂废水模板')
	begin 
		drop table temp_污水处理厂废水模板
	end
	select top 0 * into temp_污水处理厂废水模板 from 污水处理厂废水模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_污水处理厂基本模板')
	begin 
		drop table temp_污水处理厂基本模板
	end
	select top 0 * into temp_污水处理厂基本模板 from 污水处理厂基本模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_工业信息基本模板')
	begin 
		drop table temp_工业信息基本模板
	end
	select top 0 * into temp_工业信息基本模板 from 工业信息基本模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_固废信息基本模板')
	begin 
		drop table temp_固废信息基本模板
	end
	select top 0 * into temp_固废信息基本模板 from 固废信息基本模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_污水处理厂比对基本模板')
	begin 
		drop table temp_污水处理厂比对基本模板
	end
	select top 0 * into temp_污水处理厂比对基本模板 from 污水处理厂比对基本模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_工业废气比对基本模板')
	begin 
		drop table temp_工业废气比对基本模板
	end
	select top 0 * into temp_工业废气比对基本模板 from 工业废气比对基本模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_工业废水比对基本模板')
	begin 
		drop table temp_工业废水比对基本模板
	end
	select top 0 * into temp_工业废水比对基本模板 from 工业废水比对基本模板
go
IF EXISTS (SELECT * FROM sys.tables WHERE name='temp_无组织废气基本模板')
	begin 
		drop table temp_无组织废气基本模板
	end
	select top 0 * into temp_无组织废气基本模板 from 无组织废气基本模板
go

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PrepareData]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PrepareData]
GO


--=================================
-- Author:      hanfei
-- Date:        2011-10-22
-- Description: 数据查询模块：准备数据
--=================================
CREATE procedure [dbo].[PrepareData]
as
--废气生产情况模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_废气生产情况模板')
	begin 
		select * into temp_废气生产情况模板 from 废气生产情况模板
	end
Else
	begin
		truncate table temp_废气生产情况模板
		insert into temp_废气生产情况模板 select * from 废气生产情况模板
	end
--废水生产情况模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_废水生产情况模板')
	begin 
		select * into temp_废水生产情况模板 from 废水生产情况模板
	end
Else
	begin
		truncate table temp_废水生产情况模板
		insert into temp_废水生产情况模板 select * from 废水生产情况模板
	end
--工业废气基本模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_工业废气基本模板')
	begin 
		select * into temp_工业废气基本模板 from 工业废气基本模板
	end
Else
	begin
		truncate table temp_工业废气基本模板
		insert into temp_工业废气基本模板 select * from 工业废气基本模板
	end
--工业废水基本模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_工业废气基本模板')
	begin 
		select * into temp_工业废水基本模板 from 工业废水基本模板
	end
Else
	begin
		truncate table temp_工业废水基本模板
		insert into temp_工业废水基本模板 select * from 工业废水基本模板
	end
--固废废气基本模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_固废废气基本模板')
	begin 
		select * into temp_固废废气基本模板 from 固废废气基本模板
	end
Else
	begin
		truncate table temp_固废废气基本模板
		insert into temp_固废废气基本模板 select * from 固废废气基本模板
	end
--固废废水基本模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_固废废水基本模板')
	begin 
		select * into temp_固废废水基本模板 from 固废废水基本模板
	end
Else
	begin
		truncate table temp_固废废水基本模板
		insert into temp_固废废水基本模板 select * from 固废废水基本模板
	end
--污水处理厂废水模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_污水处理厂废水模板')
	begin 
		select * into temp_污水处理厂废水模板 from 污水处理厂废水模板
	end
Else
	begin
		truncate table temp_污水处理厂废水模板
		insert into temp_污水处理厂废水模板 select * from 污水处理厂废水模板
	end
--污水处理厂基本模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_污水处理厂基本模板')
	begin 
		select * into temp_污水处理厂基本模板 from 污水处理厂基本模板
	end
Else
	begin
		truncate table temp_污水处理厂基本模板
		insert into temp_污水处理厂基本模板 select * from 污水处理厂基本模板
	end
--工业信息基本模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_工业信息基本模板')
	begin 
		select * into temp_工业信息基本模板 from 工业信息基本模板
	end
Else
	begin
		truncate table temp_工业信息基本模板
		insert into temp_工业信息基本模板 select * from 工业信息基本模板
	end
--固废信息基本模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_固废信息基本模板')
	begin 
		select * into temp_固废信息基本模板 from 固废信息基本模板
	end
Else
	begin
		truncate table temp_固废信息基本模板
		insert into temp_固废信息基本模板 select * from 固废信息基本模板
	end
--污水处理厂比对基本模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_污水处理厂比对基本模板')
	begin 
		select * into temp_污水处理厂比对基本模板 from 污水处理厂比对基本模板
	end
Else
	begin
		truncate table temp_污水处理厂比对基本模板
		insert into temp_污水处理厂比对基本模板 select * from 污水处理厂比对基本模板
	end
--工业废水比对基本模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_工业废水比对基本模板')
	begin 
		select * into temp_工业废水比对基本模板 from 工业废水比对基本模板
	end
Else
	begin
		truncate table temp_工业废水比对基本模板
		insert into temp_工业废水比对基本模板 select * from 工业废水比对基本模板
	end
--工业废气比对基本模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_工业废气比对基本模板')
	begin 
		select * into temp_工业废气比对基本模板 from 工业废气比对基本模板
	end
Else
	begin
		truncate table temp_工业废气比对基本模板
		insert into temp_工业废气比对基本模板 select * from 工业废气比对基本模板
	end
--无组织废气基本模板
IF NOT EXISTS (SELECT * FROM sys.tables WHERE name='temp_无组织废气基本模板')
	begin 
		select * into temp_无组织废气基本模板 from 无组织废气基本模板
	end
Else
	begin
		truncate table temp_无组织废气基本模板
		insert into temp_无组织废气基本模板 select * from 无组织废气基本模板
	end
	
GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RowToColumn]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[RowToColumn]
GO

-- =============================================
-- Author:		hanfei
-- Create date: 2011-11-01
-- Description:	通用行转列
-- =============================================
CREATE PROCEDURE [dbo].[RowToColumn] 
	@tablename nvarchar(50),		--表名称
	@column_list nvarchar(50),		--要显示的列名称
	@column_temp nvarchar(50),		--以哪些列为基准
	@column_value nvarchar(50),		--要进行反转的列所对应值的列名
	@group_value nvarchar(50),		--要进行反转的列名
	@group_fun nvarchar(10)='max',	--反转列计算方法
	@column_order nvarchar(50)=null	--以哪些列排序
AS
BEGIN
	declare @columntable table (columnname nvarchar(50))
	declare @str_columntable nvarchar(120)
	declare @str_head nvarchar(60)
	declare @str_tail nvarchar(max)
	declare @str_case nvarchar(max)
	declare @str_pivot nvarchar(max)
	declare @str_tmptable nvarchar(120)
	set @str_columntable = 'select distinct ' + @group_value + ' as columnname from ' + @tablename
	insert into @columntable exec(@str_columntable)	
	set @str_head = 'select '+ @column_list + ','
	set @str_tmptable = '(select ' + @column_temp + ' from ' + @tablename + ') as t'
	set @str_case = ''
	set @str_pivot = ''
	select @str_case = @str_case + 'isnull([' + columnname + '],0) as ['+columnname+'],' from @columntable
	select @str_pivot = @str_pivot + '[' + columnname + '],' from @columntable
	if(len(@str_case)>0)
		begin
			set @str_case = left(@str_case,len(@str_case)-1)
			set @str_pivot = left(@str_pivot,len(@str_pivot)-1)
		end
	else
		begin
			set @str_case = '[无数据]'
			set @str_pivot = '[无数据]'
		end
	if(@column_order is not null and @column_order<>'')
	begin
		set @column_order=' order by '+@column_order
	end
	set @str_tail = ' from ' + @str_tmptable + ' pivot (' + @group_fun + '('+@column_value+') for ' + @group_value + ' in ('+@str_pivot+')) as pvt'+@column_order
	exec(@str_head+@str_case+@str_tail)
END

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Cartogram_PDFB]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Cartogram_PDFB]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-11-06
-- Description:	统计图-频度分布
--==========================================
Create procedure [dbo].[Cartogram_PDFB]
(
	@type int ,			--计算标准
	@beginYear int,		--起始年
	@endYear int,		--结束季度
	@beginQuarter int,  --起始季度
	@endQuarter int,	--结束季度
	@regionCode varchar(max),--地区编码
	@projectArea varchar(max),--专题区域编码
	@tradeCode varchar(max),--行业代码
	@whaterCode varchar(max),--受纳水体代码
	@controlType varchar(50),--废水控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int,	--统计范围
	@pollutionType int,	--监测项目类型
	@pollution varchar(50),	--监测项目编码
	@valueFrom float,	--监测值(起始)
	@valueTo float		--监测值(至)
)
as
begin

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

--设置查询条件
declare @sql nvarchar(max)
declare @strWhere nvarchar(max)
declare @accountView nvarchar(500)
declare @projcetAreaWhere nvarchar(500)
declare @regionStr nvarchar(max)
declare @tradeStr nvarchar(max)
declare @valueFromTo nvarchar(50)
set @strWhere=' where 1=1 '
set @accountView = ''
set @projcetAreaWhere = ''
set @valueFromTo = ''
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @sql='select @regionStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=14 and 代码编码 in (
					select distinct substring(代码编码,1,2)+''0000'' from 系统_代码 where 代码类型编码=14 and 代码编码 in ('+@regionCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in('+@tradeCode+')'
		set @strWhere = @strWhere + ' and 行业代码 in ('+@tradeCode+')'
		set @sql='select @tradeStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=15 and 代码编码 in (
					select distinct substring(代码编码,1,2) from 系统_代码 where 代码类型编码=15 and 代码编码 in ('+@tradeCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@tradeStr nvarchar(max) output',@tradeStr output
	end
if(@whaterCode<>'')
	 begin
		set @strWhere = @strWhere + ' and 受纳水体代码 in('+@whaterCode+')'
	 end
if(@controlType<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码 in ('+@controlType+')'
    end
if(@controlTypeGas<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码气 in ('+@controlTypeGas+')'
    end
if(@pollution<>'')
    begin
		set @strWhere = @strWhere + 'and 监测项目编号 = '''+@pollution+''''
    end
if(@statType=1)
	begin
		set @accountView=' inner join 业务_污染源名录库_总站 as z on t.监测年=z.年度 and t.行政区代码=z.行政区代码 and t.法人代码=z.法人代码 and t.详细名称=z.企业名称 and t.企业类型=z.企业类型 '
	end
if(@projectArea<>'')
	begin
		if(@type=6)
		begin
			set @projcetAreaWhere = ' where 代码编码 in ('+@projectArea+')'
		end
		set @sql='select @regionStr=(select 代码名称+'','' from 系统_代码 where 代码类型编码=33 and 代码编码 in ('+@projectArea+') for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(len(@regionStr)>0) begin set @regionStr = substring(@regionStr,1,len(@regionStr)-1) end
if(len(@tradeStr)>0) begin set @tradeStr = substring(@tradeStr,1,len(@tradeStr)-1) end

--处理时间，将时间分为监测年和季度存到临时表中
EXEC GetQuarter @beginYear,@endYear,@beginQuarter,@endQuarter,'##tmp_Quarter'

--将符合条件的监测数据先存到临时表中
if(@pollutionType=1)
begin
	set @sql = 
		'select 监测年,行政区代码,行业代码,企业代码,监测点代码,受纳水体代码,监测项目编号,季度,污染物浓度 into ##tmp_AllItem from 
		(
			select t.* from
			(
				select a.监测年,a.行政区代码,a.详细名称,a.法人代码,a.行业代码,a.企业代码,b.监测点代码,b.受纳水体代码,a.污染源控制级别代码,a.污染源控制级别代码气,c.监测项目编号,d.季度,d.污染物浓度,b.企业类型
				from dbo.业务_企业基本信息 as a 
				inner join 业务_废水排口信息 as b 
				on a.监测年=b.监测年 and a.企业代码=b.企业代码 and b.企业类型=1
				inner join 业务_企业监测点污染物信息 as c
				on b.企业代码=c.企业代码 and b.监测点代码=c.监测点代码 and b.监测年=c.监测年 and c.企业类型=b.企业类型
				inner join 业务_废水监测数据 as d 
				on d.污染物浓度 is not null and c.企业代码=d.企业代码 and c.监测点代码=d.监测点代码 
				and c.监测项目编号=d.监测项目编号 and c.监测年=d.监测年 and d.企业类型=c.企业类型
				inner join ##tmp_Quarter as q
				on d.监测年=q.监测年 and d.季度=q.季度
			) as t' + @accountView + '
		) as t' + @strWhere
end
if(@pollutionType=2)
begin
	set @sql = 
		'select 监测年,行政区代码,行业代码,企业代码,监测点代码,监测项目编号,季度,污染物浓度 into ##tmp_AllItem from 
		(
			select t.* from
			(
				select a.监测年,a.行政区代码,a.详细名称,a.法人代码,a.行业代码,a.企业代码,b.监测点代码,a.污染源控制级别代码,a.污染源控制级别代码气,c.监测项目编号,d.季度,d.污染物浓度,b.企业类型
				from dbo.业务_企业基本信息 as a 
				inner join 业务_废气监测点信息 as b 
				on a.监测年=b.监测年 and a.企业代码=b.企业代码 and b.企业类型=1
				inner join 业务_企业监测点污染物信息 as c
				on b.企业代码=c.企业代码 and b.监测点代码=c.监测点代码 and b.监测年=c.监测年 and c.企业类型=b.企业类型
				inner join 业务_废气监测数据 as d 
				on d.污染物浓度 is not null and c.企业代码=d.企业代码 and c.监测点代码=d.监测点代码 
				and c.监测项目编号=d.监测项目编号 and c.监测年=d.监测年 and d.企业类型=c.企业类型
				inner join ##tmp_Quarter as q
				on d.监测年=q.监测年 and d.季度=q.季度
			) as t' + @accountView + '
		) as t' + @strWhere
end
exec(@sql)
--处理监测值范围字符串
if(@valueFrom<0)
	begin
		if(UPPER(@pollution)='W_1') begin set @valueFrom=0 end
		else begin select @valueFrom=cast(min(污染物浓度) as float) from ##tmp_AllItem end
	end	
if(@valueTo<0)
	begin
		if(UPPER(@pollution)='W_1') begin set @valueTo=14 end
		else begin select @valueTo=cast(max(污染物浓度) as float) from ##tmp_AllItem end
	end	
--if(@valueFrom<0) begin set @valueFrom=0 end
--if(@valueTo>14) begin set @valueTo=14 end
set @valueFromTo = cast(@valueFrom as nvarchar)+'-'+cast(@valueTo as nvarchar)

--按行业统计
if(@type=1)
begin
	select @regionStr as '地区/专题区域',c.代码名称 as 行业,a.监测年,a.季度,d.监测项目名称,@valueFromTo as 监测值范围,
	case when @valueFromTo='' then '' else cast(cast(cast(a.监测次数 as decimal(18,2))/b.监测总数*100 as decimal(18,2)) as nvarchar) end as [数据频度（%）] from
	(
		select 监测年,季度,substring(行业代码,1,2) as 行业代码,监测项目编号,count(污染物浓度) as 监测次数 from ##tmp_AllItem 
		where 污染物浓度 between @valueFrom and @valueTo
		group by 监测年,季度,substring(行业代码,1,2),监测项目编号
	) as a
	inner join
	(
		select 监测年,季度,substring(行业代码,1,2) as 行业代码,监测项目编号,count(污染物浓度) as 监测总数 from ##tmp_AllItem 
		group by 监测年,季度,substring(行业代码,1,2),监测项目编号
	) as b
	on a.监测年=b.监测年 and a.季度=b.季度 and a.行业代码=b.行业代码
	inner join 系统_代码 as c
	on c.代码类型编码=15 and a.行业代码=c.代码编码
	inner join 系统_监测项目 as d 
	on a.监测项目编号=d.监测项目编号
	order by 监测年,季度,代码编码
	select 污染物浓度,cast(cast(count(污染物浓度) as decimal(18,2))/sum(count(污染物浓度)) over() as decimal(18,4))*100 as 频度 from 
	(select cast(污染物浓度 as decimal(18,1)) as 污染物浓度 from ##tmp_AllItem where 污染物浓度 between @valueFrom and @valueTo) as t
	group by 污染物浓度 order by 污染物浓度
end
--按流域统计
if(@type=2)
begin
	select @regionStr as '地区/专题区域',@tradeStr as 行业,代码名称 as 流域,a.监测年,a.季度,d.监测项目名称,@valueFromTo as 监测值范围,
	case when @valueFromTo='' then '' else cast(cast(cast(a.监测次数 as decimal(18,2))/b.监测总数*100 as decimal(18,2)) as nvarchar) end as [数据频度（%）] from
	(
		select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,监测项目编号,count(污染物浓度) as 监测次数 from ##tmp_AllItem 
		where 污染物浓度 between @valueFrom and @valueTo
		group by 监测年,季度,substring(受纳水体代码,1,1),监测项目编号
	) as a
	inner join
	(
		select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,监测项目编号,count(污染物浓度) as 监测总数 from ##tmp_AllItem 
		group by 监测年,季度,substring(受纳水体代码,1,1),监测项目编号
	) as b
	on a.监测年=b.监测年 and a.季度=b.季度 and a.受纳水体代码=b.受纳水体代码
	inner join 系统_代码 as c
	on c.代码类型编码=20 and a.受纳水体代码=c.代码编码
	inner join 系统_监测项目 as d 
	on a.监测项目编号=d.监测项目编号
	order by 监测年,季度,代码编码
	select 污染物浓度,cast(cast(count(污染物浓度) as decimal(18,2))/sum(count(污染物浓度)) over() as decimal(18,4))*100 as 频度 from 
	(select cast(污染物浓度 as decimal(18,1)) as 污染物浓度 from ##tmp_AllItem where 污染物浓度 between @valueFrom and @valueTo) as t
	group by 污染物浓度 order by 污染物浓度
end
--按水系统计
if(@type=3)
begin
	select [地区/专题区域],行业,水系,监测年,季度,监测项目名称,监测值范围,数据频度 from (
		select 代码编码,@regionStr as '地区/专题区域',@tradeStr as 行业,代码名称 as 水系,a.监测年,a.季度,d.监测项目名称,@valueFromTo as 监测值范围,
		case when @valueFromTo='' then '' else cast(cast(cast(a.监测次数 as decimal(18,2))/b.监测总数*100 as decimal(18,2)) as nvarchar) end as [数据频度（%）] from
		(
			select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,监测项目编号,count(污染物浓度) as 监测次数 from ##tmp_AllItem 
			where 污染物浓度 between @valueFrom and @valueTo
			group by 监测年,季度,substring(受纳水体代码,1,1),监测项目编号
		) as a
		inner join
		(
			select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,监测项目编号,count(污染物浓度) as 监测总数 from ##tmp_AllItem 
			group by 监测年,季度,substring(受纳水体代码,1,1),监测项目编号
		) as b
		on a.监测年=b.监测年 and a.季度=b.季度 and a.受纳水体代码=b.受纳水体代码
		inner join 系统_代码 as c
		on c.代码类型编码=20 and a.受纳水体代码=c.代码编码
		inner join 系统_监测项目 as d 
		on a.监测项目编号=d.监测项目编号
		union all
		select 代码编码,@regionStr as '地区/专题区域',@tradeStr as 行业,代码名称 as 水系,a.监测年,a.季度,d.监测项目名称,@valueFromTo as 监测值范围,
		case when @valueFromTo='' then '' else cast(cast(cast(a.监测次数 as decimal(18,2))/b.监测总数 as decimal(18,2)) as nvarchar) end as [数据频度（%）] from
		(
			select 监测年,季度,substring(受纳水体代码,1,2)+'00000000' as 受纳水体代码,监测项目编号,count(污染物浓度) as 监测次数 from ##tmp_AllItem 
			where 污染物浓度 between @valueFrom and @valueTo
			group by 监测年,季度,substring(受纳水体代码,1,2)+'00000000',监测项目编号
		) as a
		inner join
		(
			select 监测年,季度,substring(受纳水体代码,1,2)+'00000000' as 受纳水体代码,监测项目编号,count(污染物浓度) as 监测总数 from ##tmp_AllItem 
			group by 监测年,季度,substring(受纳水体代码,1,2)+'00000000',监测项目编号
		) as b
		on a.监测年=b.监测年 and a.季度=b.季度 and a.受纳水体代码=b.受纳水体代码
		inner join 系统_代码 as c
		on c.代码类型编码=16 and a.受纳水体代码=c.代码编码
		inner join 系统_监测项目 as d 
		on a.监测项目编号=d.监测项目编号
	) as t order by 监测年,季度,代码编码
	select 污染物浓度,cast(cast(count(污染物浓度) as decimal(18,2))/sum(count(污染物浓度)) over() as decimal(18,4))*100 as 频度 from 
	(select cast(污染物浓度 as decimal(18,1)) as 污染物浓度 from ##tmp_AllItem where 污染物浓度 between @valueFrom and @valueTo) as t
	group by 污染物浓度 order by 污染物浓度
end
--按地区（省/市）统计
if(@type=4)
begin
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,a.监测年,a.季度,d.监测项目名称,@valueFromTo as 监测值范围,
	case when @valueFromTo='' then '' else cast(cast(cast(a.监测次数 as decimal(18,2))/b.监测总数*100 as decimal(18,2)) as nvarchar) end as [数据频度（%）] from
	(
		select 监测年,季度,substring(行政区代码,1,2)+'0000' as 行政区代码,监测项目编号,count(污染物浓度) as 监测次数 from ##tmp_AllItem 
		where 污染物浓度 between @valueFrom and @valueTo
		group by 监测年,季度,substring(行政区代码,1,2)+'0000',监测项目编号
		union all
		select 监测年,季度,substring(行政区代码,1,4)+'00' as 行政区代码,监测项目编号,count(污染物浓度) as 监测次数 from ##tmp_AllItem 
		where 污染物浓度 between @valueFrom and @valueTo
		group by 监测年,季度,substring(行政区代码,1,4)+'00',监测项目编号
	) as a
	inner join
	(
		select 监测年,季度,substring(行政区代码,1,2)+'0000' as 行政区代码,监测项目编号,count(污染物浓度) as 监测总数 from ##tmp_AllItem 
		group by 监测年,季度,substring(行政区代码,1,2)+'0000',监测项目编号
		union all
		select 监测年,季度,substring(行政区代码,1,4)+'00' as 行政区代码,监测项目编号,count(污染物浓度) as 监测总数 from ##tmp_AllItem 
		group by 监测年,季度,substring(行政区代码,1,4)+'00',监测项目编号
	) as b
	on a.监测年=b.监测年 and a.季度=b.季度 and a.行政区代码=b.行政区代码
	inner join 系统_代码 as c
	on c.代码类型编码=14 and a.行政区代码=c.代码编码
	inner join 系统_监测项目 as d 
	on a.监测项目编号=d.监测项目编号
	order by 监测年,季度,代码编码
	select 污染物浓度,cast(cast(count(污染物浓度) as decimal(18,2))/sum(count(污染物浓度)) over() as decimal(18,4))*100 as 频度 from 
	(select cast(污染物浓度 as decimal(18,1)) as 污染物浓度 from ##tmp_AllItem where 污染物浓度 between @valueFrom and @valueTo) as t
	group by 污染物浓度 order by 污染物浓度
end
--按地区（省）统计
if(@type=5)
begin
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,a.监测年,a.季度,d.监测项目名称,@valueFromTo as 监测值范围,
	case when @valueFromTo='' then '' else cast(cast(cast(a.监测次数 as decimal(18,2))/b.监测总数*100 as decimal(18,2)) as nvarchar) end as [数据频度（%）] from
	(
		select 监测年,季度,substring(行政区代码,1,2)+'0000' as 行政区代码,监测项目编号,count(污染物浓度) as 监测次数 from ##tmp_AllItem 
		where 污染物浓度 between @valueFrom and @valueTo
		group by 监测年,季度,substring(行政区代码,1,2)+'0000',监测项目编号
	) as a
	inner join
	(
		select 监测年,季度,substring(行政区代码,1,2)+'0000' as 行政区代码,监测项目编号,count(污染物浓度) as 监测总数 from ##tmp_AllItem 
		group by 监测年,季度,substring(行政区代码,1,2)+'0000',监测项目编号
	) as b
	on a.监测年=b.监测年 and a.季度=b.季度 and a.行政区代码=b.行政区代码
	inner join 系统_代码 as c
	on c.代码类型编码=14 and a.行政区代码=c.代码编码
	inner join 系统_监测项目 as d 
	on a.监测项目编号=d.监测项目编号
	order by 监测年,季度,代码编码
	select 污染物浓度,cast(cast(count(污染物浓度) as decimal(18,2))/sum(count(污染物浓度)) over() as decimal(18,4))*100 as 频度 from 
	(select cast(污染物浓度 as decimal(18,1)) as 污染物浓度 from ##tmp_AllItem where 污染物浓度 between @valueFrom and @valueTo) as t
	group by 污染物浓度 order by 污染物浓度
end
--按专题区域统计
if(@type=6)
begin
	set @sql=
	'select c.代码编码,c.代码名称,a.监测年,a.季度,d.监测项目名称,a.监测次数,b.监测总数 into ##tmp_AeverData from
	(
		select 监测年,季度,区域类型编码,监测项目编号,count(污染物浓度) as 监测次数 from ##tmp_AllItem as a
		inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
		where 污染物浓度 between '+cast(isnull(@valueFrom,0) as nvarchar)+' and '+cast(isnull(@valueTo,0) as nvarchar)+'
		group by 监测年,季度,区域类型编码,监测项目编号
	) as a
	inner join
	(
		select 监测年,季度,区域类型编码,监测项目编号,count(污染物浓度) as 监测总数 from ##tmp_AllItem as a
		inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
		group by 监测年,季度,区域类型编码,监测项目编号
	) as b
	on a.监测年=b.监测年 and a.季度=b.季度 and a.区域类型编码=b.区域类型编码
	inner join 系统_代码 as c
	on c.代码类型编码=33 and c.ID=b.区域类型编码
	inner join 系统_监测项目 as d 
	on a.监测项目编号=d.监测项目编号' + @projcetAreaWhere
	exec(@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,监测项目名称,@valueFromTo as 监测值范围,
	case when isnull(@valueFromTo,'')='' then '' else cast(cast(cast(监测次数 as decimal(18,2))/监测总数*100 as decimal(18,2)) as nvarchar) end as [数据频度（%）] from
	##tmp_AeverData order by 监测年,季度,代码编码;
	select 污染物浓度,cast(cast(count(污染物浓度) as decimal(18,2))/sum(count(污染物浓度)) over() as decimal(18,4))*100 as 频度 from 
	(select cast(污染物浓度 as decimal(18,1)) as 污染物浓度 from ##tmp_AllItem where 污染物浓度 between @valueFrom and @valueTo) as t
	group by 污染物浓度 order by 污染物浓度
end

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

end


GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Cartogram_PJND]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Cartogram_PJND]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-11-07
-- Description:	统计图-监测项目平均浓度
--==========================================
CREATE procedure [dbo].[Cartogram_PJND]
(
	@type int ,			--计算标准
	@beginYear int,		--起始年
	@endYear int,		--结束季度
	@beginQuarter int,  --起始季度
	@endQuarter int,	--结束季度
	@regionCode varchar(max),--地区编码
	@projectArea varchar(max),--专题区域编码
	@tradeCode varchar(max),--行业代码
	@whaterCode varchar(max),--受纳水体代码
	@controlType varchar(50),--废水控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int,	--统计范围
	@pollutionType int,	--监测项目类型
	@pollution varchar(50)	--监测项目编码
)
as
begin

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

--设置查询条件
declare @sql nvarchar(max)
declare @strWhere nvarchar(max)
declare @accountView nvarchar(500)
declare @projcetAreaWhere nvarchar(500)
declare @regionStr nvarchar(max)
declare @tradeStr nvarchar(max)
set @strWhere=' where 1=1 '
set @accountView = ''
set @projcetAreaWhere = ''
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @sql='select @regionStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=14 and 代码编码 in (
					select distinct substring(代码编码,1,2)+''0000'' from 系统_代码 where 代码类型编码=14 and 代码编码 in ('+@regionCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in('+@tradeCode+')'
		set @strWhere = @strWhere + ' and 行业代码 in ('+@tradeCode+')'
		set @sql='select @tradeStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=15 and 代码编码 in (
					select distinct substring(代码编码,1,2) from 系统_代码 where 代码类型编码=15 and 代码编码 in ('+@tradeCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@tradeStr nvarchar(max) output',@tradeStr output
	end
if(@whaterCode<>'')
	 begin
		set @strWhere = @strWhere + ' and 受纳水体代码 in('+@whaterCode+')'
	 end
if(@controlType<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码 in ('+@controlType+')'
    end
if(@controlTypeGas<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码气 in ('+@controlTypeGas+')'
    end
if(@pollution<>'')
    begin
		set @strWhere = @strWhere + 'and 监测项目编号 = '''+@pollution+''''
    end
if(@statType=1)
	begin
		set @accountView=' inner join 业务_污染源名录库_总站 as z on t.监测年=z.年度 and t.行政区代码=z.行政区代码 and t.法人代码=z.法人代码 and t.详细名称=z.企业名称 and t.企业类型=z.企业类型 '
	end
if(@projectArea<>'')
	begin
		if(@type=6)
		begin
			set @projcetAreaWhere = ' where 代码编码 in ('+@projectArea+')'
		end
		set @sql='select @regionStr=(select 代码名称+'','' from 系统_代码 where 代码类型编码=33 and 代码编码 in ('+@projectArea+') for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(len(@regionStr)>0) begin set @regionStr = substring(@regionStr,1,len(@regionStr)-1) end
if(len(@tradeStr)>0) begin set @tradeStr = substring(@tradeStr,1,len(@tradeStr)-1) end

--处理时间，将时间分为监测年和季度存到临时表中
declare @quarter table(季度 int)
insert into @quarter
select 1 季度 union select 2 季度 union select 3 季度 union select 4 季度
--创建临时表
create table ##tmp_Quarter(监测年 int,季度 int)
declare @tempYear int
set @tempYear=@beginYear
while(@tempYear<=@endYear)
	begin
		if(@beginYear<>@endYear)
		begin
			if(@tempYear=@beginYear)
			begin
				insert into ##tmp_Quarter select @tempYear as 监测年,季度 from @quarter where 季度>=@beginQuarter
			end
			if(@tempYear=@endYear)
			begin
				insert into ##tmp_Quarter select @tempYear as 监测年,季度 from @quarter where 季度<=@endQuarter
			end
			if(@tempYear>@beginYear and @tempYear<@endYear)
			begin
				insert into ##tmp_Quarter select @tempYear as 监测年,季度 from @quarter
			end
		end
		else
		begin
			insert into ##tmp_Quarter select @tempYear as 监测年,季度 from @quarter where 季度 between @beginQuarter and @endQuarter
		end
		set @tempYear = @tempYear+1
	end

--将符合条件的监测数据先存到临时表中
if(@pollutionType=1)
begin
	set @sql = 
	'select 监测年,行政区代码,行业代码,企业代码,受纳水体代码,监测点代码,监测项目编号,季度,污染物浓度 into ##tmp_AllItem from 
	(
		select t.* from
		(
			select a.监测年,a.行政区代码,a.详细名称,a.法人代码,a.行业代码,a.企业代码,b.受纳水体代码,b.监测点代码,a.污染源控制级别代码,a.污染源控制级别代码气,c.监测项目编号,d.季度,avg(d.污染物浓度) as 污染物浓度,b.企业类型
			from dbo.业务_企业基本信息 as a 
			inner join 业务_废水排口信息 as b
			on a.监测年=b.监测年 and a.企业代码=b.企业代码 and b.企业类型=1
			inner join 业务_企业监测点污染物信息 as c
			on b.企业代码=c.企业代码 and b.监测点代码=c.监测点代码 and b.监测年=c.监测年 and c.企业类型=b.企业类型
			inner join 业务_废水监测数据 as d 
			on d.污染物浓度 is not null and c.企业代码=d.企业代码 and c.监测点代码=d.监测点代码
			and c.监测项目编号=d.监测项目编号 and c.监测年=d.监测年 and d.企业类型=c.企业类型
			inner join ##tmp_Quarter as q
			on d.监测年=q.监测年 and d.季度=q.季度
			group by a.监测年,a.行政区代码,a.行业代码,a.企业代码,a.详细名称,a.法人代码,b.受纳水体代码,b.监测点代码,a.污染源控制级别代码,a.污染源控制级别代码气,c.监测项目编号,d.季度,b.企业类型
		) as t' + @accountView + '
	) as t' + @strWhere
end
if(@pollutionType=2)
begin
	set @sql = 
	'select 监测年,行政区代码,行业代码,企业代码,监测点代码,监测项目编号,季度,污染物浓度 into ##tmp_AllItem from 
	(
		select t.* from
		(
			select a.监测年,a.行政区代码,a.详细名称,a.法人代码,a.行业代码,a.企业代码,b.监测点代码,a.污染源控制级别代码,a.污染源控制级别代码气,c.监测项目编号,d.季度,avg(d.污染物浓度) as 污染物浓度,b.企业类型
			from dbo.业务_企业基本信息 as a 
			inner join 业务_废气监测点信息 as b 
			on a.监测年=b.监测年 and a.企业代码=b.企业代码 and b.企业类型=1
			inner join 业务_企业监测点污染物信息 as c
			on b.企业代码=c.企业代码 and b.监测点代码=c.监测点代码 and b.监测年=c.监测年 and c.企业类型=b.企业类型
			inner join 业务_废气监测数据 as d 
			on d.污染物浓度 is not null and c.企业代码=d.企业代码 and c.监测点代码=d.监测点代码 
			and c.监测项目编号=d.监测项目编号 and c.监测年=d.监测年 and d.企业类型=c.企业类型
			inner join ##tmp_Quarter as q
			on d.监测年=q.监测年 and d.季度=q.季度
			group by a.监测年,a.行政区代码,a.行业代码,a.企业代码,a.详细名称,a.法人代码,b.监测点代码,a.污染源控制级别代码,a.污染源控制级别代码气,c.监测项目编号,d.季度,b.企业类型
		) as t' + @accountView + '
	) as t' + @strWhere
end
exec(@sql)

--按行业统计
if(@type=1)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,污染物浓度 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.污染物浓度 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(行业代码,1,2) as 行业代码,监测项目编号,avg(污染物浓度) as 污染物浓度 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(行业代码,1,2)
		 ) as b 
		 on a.代码类型编码=15 and a.代码编码=b.行业代码
		 inner join 系统_监测项目 as c
		 on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',代码名称 as 行业,监测年,季度,监测项目名称,污染物浓度 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,污染物浓度','污染物浓度','代码名称','avg','监测年,季度'
end
--按流域统计
if(@type=2)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,污染物浓度 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.污染物浓度 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,监测项目编号,avg(污染物浓度) as 污染物浓度 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(受纳水体代码,1,1)
		 ) as b 
		 on a.代码类型编码=20 and a.代码编码=b.受纳水体代码
		 inner join 系统_监测项目 as c
		 on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',@tradeStr as 行业,代码名称 as 流域,监测年,季度,监测项目名称,污染物浓度
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,污染物浓度','污染物浓度','代码名称','avg','监测年,季度'
end
--按水系统计
if(@type=3)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,污染物浓度 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.污染物浓度 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,监测项目编号,avg(污染物浓度) as 污染物浓度 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(受纳水体代码,1,1)
		 ) as b 
		 on a.代码类型编码=20 and a.代码编码=b.受纳水体代码
		 inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
		union all
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.污染物浓度 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(受纳水体代码,1,2)+''00000000'' as 受纳水体代码,监测项目编号,avg(污染物浓度) as 污染物浓度 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(受纳水体代码,1,2)+''00000000''
		 ) as b 
		 on a.代码类型编码=16 and a.代码编码=b.受纳水体代码
		 inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',@tradeStr as 行业,代码名称 as 水系,监测年,季度,监测项目名称,污染物浓度
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,污染物浓度','污染物浓度','代码名称','avg','监测年,季度'
end
--按地区（省/市）统计
if(@type=4)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,污染物浓度 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.污染物浓度 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,监测项目编号,avg(污染物浓度) as 污染物浓度 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(行政区代码,1,2)+''0000''
			union all
			select 监测年,季度,substring(行政区代码,1,4)+''00'' as 行政区代码,监测项目编号,avg(污染物浓度) as 污染物浓度 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(行政区代码,1,4)+''00''
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
		 inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,监测项目名称,污染物浓度 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,污染物浓度','污染物浓度','代码名称','avg','监测年,季度'
end
--按地区（省）统计
if(@type=5)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,污染物浓度 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.污染物浓度 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,监测项目编号,avg(污染物浓度) as 污染物浓度 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(行政区代码,1,2)+''0000''
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
		 inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,监测项目名称,污染物浓度 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,污染物浓度','污染物浓度','代码名称','avg','监测年,季度'
end
--按专题区域统计
if(@type=6)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,污染物浓度 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.污染物浓度 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,b.区域类型编码,监测项目编号,avg(污染物浓度) as 污染物浓度 
			from ##tmp_AllItem as a
			inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
			group by 监测年,季度,监测项目编号,b.区域类型编码
		) as b 
		on a.代码类型编码=33 and a.ID=b.区域类型编码
		inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
	) as t' + @projcetAreaWhere
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,监测项目名称,污染物浓度 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,污染物浓度','污染物浓度','代码名称','avg','监测年,季度'
end

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

end


GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Cartogram_WPF]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Cartogram_WPF]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-11-07
-- Description:	统计图-污染物排放量
--==========================================
CREATE procedure [dbo].[Cartogram_WPF]
(
	@type int ,			--计算标准
	@beginYear int,		--起始年
	@endYear int,		--结束季度
	@beginQuarter int,  --起始季度
	@endQuarter int,	--结束季度
	@regionCode varchar(max),--地区编码
	@projectArea varchar(max),--专题区域编码
	@tradeCode varchar(max),--行业代码
	@whaterCode varchar(max),--受纳水体代码
	@controlType varchar(50),--废水控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int,	--统计范围
	@pollutionType int,	--监测项目类型
	@pollution varchar(50)	--监测项目编码
)
as
begin

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

--设置查询条件
declare @sql nvarchar(max)
declare @strWhere nvarchar(max)
declare @accountView nvarchar(500)
declare @projcetAreaWhere nvarchar(500)
declare @regionStr nvarchar(max)
declare @tradeStr nvarchar(max)
set @strWhere=' where 1=1 '
set @accountView = ''
set @projcetAreaWhere = ''
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @sql='select @regionStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=14 and 代码编码 in (
					select distinct substring(代码编码,1,2)+''0000'' from 系统_代码 where 代码类型编码=14 and 代码编码 in ('+@regionCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in('+@tradeCode+')'
		set @strWhere = @strWhere + ' and 行业代码 in ('+@tradeCode+')'
		set @sql='select @tradeStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=15 and 代码编码 in (
					select distinct substring(代码编码,1,2) from 系统_代码 where 代码类型编码=15 and 代码编码 in ('+@tradeCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@tradeStr nvarchar(max) output',@tradeStr output
	end
if(@whaterCode<>'')
	 begin
		set @strWhere = @strWhere + ' and 受纳水体代码 in('+@whaterCode+')'
	 end

if(@controlType<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码 in ('+@controlType+')'
    end
if(@controlTypeGas<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码气 in ('+@controlTypeGas+')'
    end
if(@pollution<>'')
    begin
		set @strWhere = @strWhere + 'and 监测项目编号 = '''+@pollution+''''
    end
if(@statType=1)
	begin
		if(@pollutionType=1) begin set @accountView='V_废水排放量计算_名录库' end
		else begin set @accountView='V_废气排放量计算_名录库' end
	end
if(@statType=2)
	begin
		if(@pollutionType=1) begin set @accountView='V_废水排放量计算' end
		else begin set @accountView='V_废气排放量计算' end
	end
if(@projectArea<>'')
	begin
		if(@type=6)
		begin
			set @projcetAreaWhere = ' where 代码编码 in ('+@projectArea+')'
		end
		set @sql='select @regionStr=(select 代码名称+'','' from 系统_代码 where 代码类型编码=33 and 代码编码 in ('+@projectArea+') for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(len(@regionStr)>0) begin set @regionStr = substring(@regionStr,1,len(@regionStr)-1) end
if(len(@tradeStr)>0) begin set @tradeStr = substring(@tradeStr,1,len(@tradeStr)-1) end

--处理时间，将时间分为监测年和季度存到临时表中
EXEC GetQuarter @beginYear,@endYear,@beginQuarter,@endQuarter,'##tmp_Quarter'

--将符合条件的监测数据先存到临时表中
if(@pollutionType=1)
begin
	set @sql = 
	'select 监测年,季度,行业代码,行政区代码,受纳水体代码,企业代码,监测项目编号,avg(排放量) as 排放量 into ##tmp_AllItem from
	(
		select a.监测年,a.季度,行业代码,行政区代码,企业代码,受纳水体代码,污染物浓度*废水排放量*生产总天数/工况负荷*平均工况负荷/1000 as 排放量,监测项目编号
		from '+@accountView+
		' as a inner join ##tmp_Quarter as q
		on a.监测年=q.监测年 and a.季度=q.季度'+@strWhere+'
	) as t
	group by 监测年,季度,行业代码,行政区代码,企业代码,受纳水体代码,监测项目编号'
end
if(@pollutionType=2)
begin
	set @sql = 
	'select 监测年,季度,行业代码,行政区代码,企业代码,监测项目编号,avg(排放量) as 排放量 into ##tmp_AllItem from
	(
		select a.监测年,a.季度,行业代码,行政区代码,企业代码,污染物浓度*废气排放量/工况负荷*生产总小时数*平均工况负荷/1000000 as 排放量,监测项目编号
		from '+@accountView+
		' as a inner join ##tmp_Quarter as q
		on a.监测年=q.监测年 and a.季度=q.季度'+@strWhere+'
	) as t
	group by 监测年,季度,行业代码,行政区代码,企业代码,监测项目编号'
end
print(@sql)
exec(@sql)

--按行业统计
if(@type=1)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.排放量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(行业代码,1,2) as 行业代码,监测项目编号,sum(排放量) as 排放量 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(行业代码,1,2)
		 ) as b 
		 on a.代码类型编码=15 and a.代码编码=b.行业代码
		 inner join 系统_监测项目 as c
		 on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',代码名称 as 行业,监测年,季度,监测项目名称,排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,排放量','排放量','代码名称','avg','监测年,季度'
end
--按流域统计
if(@type=2)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,监测项目编号,sum(排放量) as 排放量 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(受纳水体代码,1,1)
		 ) as b 
		 on a.代码类型编码=20 and a.代码编码=b.受纳水体代码
		 inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',@tradeStr as 行业,代码名称 as 流域,监测年,季度,监测项目名称,排放量
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,排放量','排放量','代码名称','avg','监测年,季度'
end
--按水系统计
if(@type=3)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,监测项目编号,sum(排放量) as 排放量 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(受纳水体代码,1,1)
		 ) as b 
		 on a.代码类型编码=20 and a.代码编码=b.受纳水体代码
		 inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
		union all
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(受纳水体代码,1,2)+''00000000'' as 受纳水体代码,监测项目编号,sum(排放量) as 排放量 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(受纳水体代码,1,2)+''00000000''
		 ) as b 
		 on a.代码类型编码=16 and a.代码编码=b.受纳水体代码
		 inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',@tradeStr as 行业,代码名称 as 水系,监测年,季度,监测项目名称,排放量
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,排放量','排放量','代码名称','avg','监测年,季度'
end
--按地区（省/市）统计
if(@type=4)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,监测项目编号,sum(排放量) as 排放量 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(行政区代码,1,2)+''0000''
			union all
			select 监测年,季度,substring(行政区代码,1,4)+''00'' as 行政区代码,监测项目编号,sum(排放量) as 排放量 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(行政区代码,1,4)+''00''
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
		 inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,监测项目名称,排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,排放量','排放量','代码名称','avg','监测年,季度'
end
--按地区（省）统计
if(@type=5)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,监测项目编号,sum(排放量) as 排放量 
			from ##tmp_AllItem
			group by 监测年,季度,监测项目编号,substring(行政区代码,1,2)+''0000''
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
		 inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,监测项目名称,排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,排放量','排放量','代码名称','avg','监测年,季度'
end
--按专题区域统计
if(@type=6)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,b.区域类型编码,监测项目编号,sum(排放量) as 排放量 
			from ##tmp_AllItem as a
			inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
			group by 监测年,季度,监测项目编号,b.区域类型编码
		) as b 
		on a.代码类型编码=33 and a.ID=b.区域类型编码
		inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
	) as t' + @projcetAreaWhere
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,监测项目名称,排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,排放量','排放量','代码名称','avg','监测年,季度'
end

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Cartogram_BDSJ]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Cartogram_BDSJ]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-11-04
-- Description:	统计图-比对数据
--==========================================
CREATE procedure [dbo].[Cartogram_BDSJ]
(
	@type int ,			--计算标准
	@beginYear int,		--起始年
	@endYear int,		--结束季度
	@beginQuarter int,  --起始季度
	@endQuarter int,	--结束季度
	@regionCode varchar(max),--地区编码
	@projectArea varchar(max),--专题区域编码
	@tradeCode varchar(max),--行业代码
	@whaterCode varchar(max),--受纳水体代码
	@controlType varchar(50),--废水控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int,	--统计范围
	@pollutionType int,	--监测项目类型
	@pollution varchar(50)	--监测项目编码
)
as
begin

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

--设置查询条件
declare @sql nvarchar(max)
declare @strWhere nvarchar(max)
declare @accountView nvarchar(500)
declare @projcetAreaWhere nvarchar(500)
declare @regionStr nvarchar(max)
declare @tradeStr nvarchar(max)
set @strWhere=' where 1=1 '
set @accountView = ''
set @projcetAreaWhere = ''
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @sql='select @regionStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=14 and 代码编码 in (
					select distinct substring(代码编码,1,2)+''0000'' from 系统_代码 where 代码类型编码=14 and 代码编码 in ('+@regionCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in('+@tradeCode+')'
		set @strWhere = @strWhere + ' and 行业代码 in ('+@tradeCode+')'
		set @sql='select @tradeStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=15 and 代码编码 in (
					select distinct substring(代码编码,1,2) from 系统_代码 where 代码类型编码=15 and 代码编码 in ('+@tradeCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@tradeStr nvarchar(max) output',@tradeStr output
	end
if(@whaterCode<>'')
	 begin
		set @strWhere = @strWhere + ' and 受纳水体代码 in('+@whaterCode+')'
	 end
if(@controlType<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码 in ('+@controlType+')'
    end
if(@controlTypeGas<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码气 in ('+@controlTypeGas+')'
    end
if(@pollution<>'')
    begin
		set @strWhere = @strWhere + 'and 监测项目编号 = '''+@pollution+''''
    end
if(@statType=1)
	begin
		set @accountView=' inner join 业务_污染源名录库_总站 as z on t.监测年=z.年度 and t.行政区代码=z.行政区代码 and t.法人代码=z.法人代码 and t.详细名称=z.企业名称 and t.企业类型=z.企业类型 '
	end
if(@projectArea<>'')
	begin
		if(@type=6)
		begin
			set @projcetAreaWhere = ' where 代码编码 in ('+@projectArea+')'
		end
		set @sql='select @regionStr=(select 代码名称+'','' from 系统_代码 where 代码类型编码=33 and 代码编码 in ('+@projectArea+') for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(len(@regionStr)>0) begin set @regionStr = substring(@regionStr,1,len(@regionStr)-1) end
if(len(@tradeStr)>0) begin set @tradeStr = substring(@tradeStr,1,len(@tradeStr)-1) end


--处理时间，将时间分为监测年和季度存到临时表中
EXEC GetQuarter @beginYear,@endYear,@beginQuarter,@endQuarter,'##tmp_Quarter'

--将符合条件的监测数据先存到临时表中
if(@pollutionType=1)
begin
	set @sql = 
	'select 监测年,行政区代码,行业代码,企业代码,监测点代码,受纳水体代码,监测项目编号,季度,手工监测数据,自动监测数据,监测设备ID into ##tmp_AllItem from 
	(
		select t.* from
		(
			select a.监测年,a.行政区代码,a.详细名称,a.法人代码,a.行业代码,a.企业代码,b.监测点代码,a.污染源控制级别代码,a.污染源控制级别代码气,c.受纳水体代码,b.监测项目编号,q.季度,b.手工监测数据,b.自动监测数据,b.监测设备ID,b.企业类型
			from dbo.业务_企业基本信息 as a 
			inner join 
			(select 监测年,case when 监测月 in (1,2,3) then 1
				when 监测月 in (4,5,6) then 2
				when 监测月 in (7,8,9) then 3
				when 监测月 in (10,11,12) then 4
				else 0 end as 季度,企业代码,企业类型,监测点代码,监测项目编号,手工监测数据,自动监测数据,监测设备ID from 业务_废水在线比对) as b 
			on a.监测年=b.监测年 and a.企业代码=b.企业代码 and b.企业类型=1
			inner join 业务_废水排口信息 as c
			on b.监测年=c.监测年 and b.企业代码=c.企业代码 and b.监测点代码=c.监测点代码 and b.企业类型=c.企业类型
			inner join ##tmp_Quarter as q
			on b.监测年=q.监测年 and b.季度=q.季度
		) as t' + @accountView + '
	) as t' + @strWhere
end
if(@pollutionType=2)
begin
	set @sql = 
	'select 监测年,行政区代码,行业代码,企业代码,监测点代码,监测项目编号,季度,手工监测数据,自动监测数据,监测设备ID into ##tmp_AllItem from 
	(
		select t.* from
		(
			select a.监测年,a.行政区代码,a.详细名称,a.法人代码,a.行业代码,a.企业代码,b.监测点代码,a.污染源控制级别代码,a.污染源控制级别代码气,b.监测项目编号,q.季度,b.手工监测数据,b.自动监测数据,b.监测设备ID,b.企业类型
			from dbo.业务_企业基本信息 as a 
			inner join 
			(select 监测年,case when 监测月 in (1,2,3) then 1
				when 监测月 in (4,5,6) then 2
				when 监测月 in (7,8,9) then 3
				when 监测月 in (10,11,12) then 4
				else 0 end as 季度,企业代码,企业类型,监测点代码,监测项目编号,手工监测数据,自动监测数据,监测设备ID from 业务_废气在线比对) as b 
			on a.监测年=b.监测年 and a.企业代码=b.企业代码 and b.企业类型=1
			inner join 业务_废气监测点信息 as c
			on b.监测年=c.监测年 and b.企业代码=c.企业代码 and b.监测点代码=c.监测点代码 and b.企业类型=c.企业类型
			inner join ##tmp_Quarter as q
			on b.监测年=q.监测年 and b.季度=q.季度
		) as t' + @accountView + '
	) as t' + @strWhere
end
exec(@sql)

--按行业统计
if(@type=1)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,监测设备数量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.监测设备数量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,行业代码,监测项目编号,count(监测项目编号) as 监测设备数量 from
			(
				 select distinct 监测年,季度,substring(行业代码,1,2) as 行业代码,企业代码,监测点代码,监测项目编号,监测设备ID from ##tmp_AllItem
			) as a
			group by 监测年,季度,行业代码,监测项目编号
		) as b 
		on a.代码类型编码=15 and a.代码编码=b.行业代码
		inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',代码名称 as 行业,监测年,季度,监测项目名称,监测设备数量
	from ##tmp_AeverData order by 监测年,季度,代码编码
	select 手工监测数据,自动监测数据 from ##tmp_AllItem
end
--按流域统计
if(@type=2)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,监测设备数量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.监测设备数量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,受纳水体代码,监测项目编号,count(监测项目编号) as 监测设备数量 from
			(
				select distinct 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,企业代码,监测点代码,监测项目编号,监测设备ID from ##tmp_AllItem
			) as a
			group by 监测年,季度,受纳水体代码,监测项目编号
		 ) as b 
		 on a.代码类型编码=20 and a.代码编码=b.受纳水体代码
		 inner join 系统_监测项目 as c
		 on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',@tradeStr as 行业,代码名称 as 流域,监测年,季度,监测项目名称,监测设备数量
	from ##tmp_AeverData order by 监测年,季度,代码编码
	select 手工监测数据,自动监测数据 from ##tmp_AllItem
end
--按水系统计
if(@type=3)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,监测设备数量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.监测设备数量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,受纳水体代码,监测项目编号,count(监测项目编号) as 监测设备数量 from
			(
				select distinct 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,企业代码,监测点代码,监测项目编号,监测设备ID from ##tmp_AllItem
			) as a
			group by 监测年,季度,受纳水体代码,监测项目编号
		 ) as b 
		on a.代码类型编码=20 and a.代码编码=b.受纳水体代码
		inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
		union all
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.监测设备数量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,受纳水体代码,监测项目编号,count(监测项目编号) as 监测设备数量 from
			(
				select distinct 监测年,季度,substring(受纳水体代码,1,2)+''00000000'' as 受纳水体代码,企业代码,监测点代码,监测项目编号,监测设备ID from ##tmp_AllItem
			) as a
			group by 监测年,季度,受纳水体代码,监测项目编号
		 ) as b 
		 on a.代码类型编码=16 and a.代码编码=b.受纳水体代码
		 inner join 系统_监测项目 as c
		on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',@tradeStr as 行业,代码名称 as 水系,监测年,季度,监测项目名称,监测设备数量
	from ##tmp_AeverData order by 监测年,季度,代码编码
	select 手工监测数据,自动监测数据 from ##tmp_AllItem
end
--按地区（省/市）统计
if(@type=4)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,监测设备数量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.监测设备数量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,行政区代码,监测项目编号,count(监测项目编号) as 监测设备数量
			from
			(
				 select distinct 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,企业代码,监测点代码,监测项目编号,监测设备ID from ##tmp_AllItem
			) as a
			group by 监测年,季度,行政区代码,监测项目编号
			union all
			select 监测年,季度,行政区代码,监测项目编号,count(监测项目编号) as 监测设备数量
			from
			(
				 select distinct 监测年,季度,substring(行政区代码,1,4)+''00'' as 行政区代码,企业代码,监测点代码,监测项目编号,监测设备ID from ##tmp_AllItem
			) as a
			group by 监测年,季度,行政区代码,监测项目编号
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
		 inner join 系统_监测项目 as c
		 on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,监测项目名称,监测设备数量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	select 手工监测数据,自动监测数据 from ##tmp_AllItem
end
--按地区（省）统计
if(@type=5)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,监测设备数量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.监测设备数量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,行政区代码,监测项目编号,count(监测项目编号) as 监测设备数量
			from
			(
				 select distinct 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,企业代码,监测点代码,监测项目编号,监测设备ID from ##tmp_AllItem
			) as a
			group by 监测年,季度,行政区代码,监测项目编号
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
		 inner join 系统_监测项目 as c
		 on b.监测项目编号=c.监测项目编号
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,监测项目名称,监测设备数量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	select 手工监测数据,自动监测数据 from ##tmp_AllItem
end
--按专题区域统计
if(@type=6)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,监测项目名称,监测设备数量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,c.监测项目名称,b.监测设备数量 from
		系统_代码 as a
		inner join
		(
			select 区域类型编码,监测年,季度,监测项目编号,count(监测项目编号) as 监测设备数量 from
			(
				 select distinct b.区域类型编码,监测年,季度,行政区代码,企业代码,监测点代码,监测项目编号,监测设备ID from ##tmp_AllItem as a
				 inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
			) as a
			group by 监测年,季度,区域类型编码,监测项目编号
		 ) as b 
		 on a.代码类型编码=33 and a.ID=b.区域类型编码
		 inner join 系统_监测项目 as c
		 on b.监测项目编号=c.监测项目编号
	) as t' + @projcetAreaWhere
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,监测项目名称,监测设备数量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	select 手工监测数据,自动监测数据 from ##tmp_AllItem
end

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Cartogram_MHLL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Cartogram_MHLL]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-11-04
-- Description:	统计图-煤含硫率
--==========================================
CREATE procedure [dbo].[Cartogram_MHLL]
(
	@type int ,			--计算标准
	@beginYear int,		--起始年
	@endYear int,		--结束季度
	@beginQuarter int,  --起始季度
	@endQuarter int,	--结束季度
	@regionCode varchar(max),--地区编码
	@projectArea varchar(max),--专题区域编码
	@tradeCode varchar(max),--行业代码
	@whaterCode varchar(max),--受纳水体代码
	@controlType varchar(50),--废水控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int	--统计范围
)
as
begin

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllEnter') is not null begin drop table ##tmp_AllEnter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

--设置查询条件
declare @sql nvarchar(max)
declare @strWhere nvarchar(max)
declare @accountView nvarchar(500)
declare @projcetAreaWhere nvarchar(500)
declare @regionStr nvarchar(max)
declare @tradeStr nvarchar(max)
set @strWhere=' where 1=1 '
set @accountView = ''
set @projcetAreaWhere = ''
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @sql='select @regionStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=14 and 代码编码 in (
					select distinct substring(代码编码,1,2)+''0000'' from 系统_代码 where 代码类型编码=14 and 代码编码 in ('+@regionCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in ('+@tradeCode+')'
		set @sql='select @tradeStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=15 and 代码编码 in (
					select distinct substring(代码编码,1,2) from 系统_代码 where 代码类型编码=15 and 代码编码 in ('+@tradeCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@tradeStr nvarchar(max) output',@tradeStr output
	end
if(@controlType<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码 in ('+@controlType+')'
    end
if(@controlTypeGas<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码气 in ('+@controlTypeGas+')'
    end
if(@statType=1)
	begin
		set @accountView=' inner join 业务_污染源名录库_总站 as z on t.监测年=z.年度 and t.行政区代码=z.行政区代码 and t.法人代码=z.法人代码 and t.详细名称=z.企业名称 and t.企业类型=z.企业类型 '
	end
if(@projectArea<>'')
	begin
		if(@type=6)
		begin
			set @projcetAreaWhere = ' where 代码编码 in ('+@projectArea+')'
		end
		set @sql='select @regionStr=(select 代码名称+'','' from 系统_代码 where 代码类型编码=33 and 代码编码 in ('+@projectArea+') for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(len(@regionStr)>0) begin set @regionStr = substring(@regionStr,1,len(@regionStr)-1) end
if(len(@tradeStr)>0) begin set @tradeStr = substring(@tradeStr,1,len(@tradeStr)-1) end


--处理时间，将时间分为监测年和季度存到临时表中
EXEC GetQuarter @beginYear,@endYear,@beginQuarter,@endQuarter,'##tmp_Quarter'

--将符合条件的监测数据先存到临时表中
set @sql = 
	'select 监测年,行政区代码,行业代码,企业代码,季度,煤含硫率,季度用煤总量 into ##tmp_AllItem from 
	(
		select t.* from
		(
			select a.监测年,a.行政区代码,a.详细名称,a.法人代码,a.行业代码,a.企业代码,a.污染源控制级别代码,a.污染源控制级别代码气,b.季度,煤含硫率,季度用煤总量,''1'' as 企业类型
			from dbo.业务_企业基本信息 as a 
			inner join 业务_煤含硫率 as b 
			on a.监测年=b.监测年 and a.企业代码=b.企业代码
			inner join ##tmp_Quarter as q
			on b.监测年=q.监测年 and b.季度=q.季度
		) as t' + @accountView + '
	) as t' + @strWhere
print(@sql)
exec(@sql)

--按行业统计
if(@type=1)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,煤含硫率 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,case when isnull(b.煤含硫率,0) =0 then 0 else b.煤含硫率/b.季度用煤总量*100 end as 煤含硫率 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(行业代码,1,2) as 行业代码,count(distinct 企业代码) as 企业数量,sum(煤含硫率*季度用煤总量) as 煤含硫率,sum(季度用煤总量) as 季度用煤总量
			from ##tmp_AllItem
			group by 监测年,季度,substring(行业代码,1,2)
		) as b 
		on a.代码类型编码=15 and a.代码编码=b.行业代码
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',代码名称 as 行业,监测年,季度,企业数量,煤含硫率 as [煤含硫率（%）]
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,煤含硫率','煤含硫率','代码名称','avg','监测年,季度'
end
--按地区（省/市）统计
if(@type=4)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,煤含硫率 into ##tmp_AeverData from
	(
		 select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,case when isnull(b.煤含硫率,0) =0 then 0 else b.煤含硫率/b.季度用煤总量*100 end as 煤含硫率 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(行政区代码,1,4)+''00'' as 行政区代码,count(distinct 企业代码) as 企业数量,sum(煤含硫率*季度用煤总量) as 煤含硫率,sum(季度用煤总量) as 季度用煤总量
			from ##tmp_AllItem
			group by 监测年,季度,substring(行政区代码,1,4)+''00''
			union all
			select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,count(distinct 企业代码) as 企业数量,sum(煤含硫率*季度用煤总量) as 煤含硫率,sum(季度用煤总量) as 季度用煤总量
			from ##tmp_AllItem
			group by 监测年,季度,substring(行政区代码,1,2)+''0000''
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,企业数量,煤含硫率 as [煤含硫率（%）]
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,煤含硫率','煤含硫率','代码名称','avg','监测年,季度'
end
--按地区（省）统计
if(@type=5)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,煤含硫率 into ##tmp_AeverData from
	(
		 select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,case when isnull(b.煤含硫率,0) =0 then 0 else b.煤含硫率/b.季度用煤总量*100 end as 煤含硫率 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,count(distinct 企业代码) as 企业数量,sum(煤含硫率*季度用煤总量) as 煤含硫率,sum(季度用煤总量) as 季度用煤总量
			from ##tmp_AllItem
			group by 监测年,季度,substring(行政区代码,1,2)+''0000''
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,企业数量,煤含硫率 as [煤含硫率（%）]
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,煤含硫率','煤含硫率','代码名称','avg','监测年,季度'
end
--按专题区域统计
if(@type=6)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,煤含硫率 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,case when isnull(b.煤含硫率,0) =0 then 0 else b.煤含硫率/b.季度用煤总量*100 end as 煤含硫率 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,区域类型编码,count(distinct 企业代码) as 企业数量,sum(煤含硫率*季度用煤总量) as 煤含硫率,sum(季度用煤总量) as 季度用煤总量
			from ##tmp_AllItem as a
			inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
			group by 监测年,季度,b.区域类型编码
		 ) as b 
		 on a.代码类型编码=33 and a.ID=b.区域类型编码
	) as t'+@projcetAreaWhere
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,企业数量,煤含硫率 as [煤含硫率（%）]
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,煤含硫率','煤含硫率','代码名称','avg','监测年,季度'
end

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

end

GO


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Cartogram_FQPFL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Cartogram_FQPFL]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-11-04
-- Description:	统计图-废气排放量
--==========================================
CREATE procedure [dbo].[Cartogram_FQPFL]
(
	@type int ,			--计算标准
	@beginYear int,		--起始年
	@endYear int,		--结束季度
	@beginQuarter int,  --起始季度
	@endQuarter int,	--结束季度
	@regionCode varchar(max),--地区编码
	@projectArea varchar(max),--专题区域编码
	@tradeCode varchar(max),--行业代码
	@whaterCode varchar(max),--受纳水体代码
	@controlType varchar(50),--废水控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int	--统计范围
)
as
begin

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

--设置查询条件
declare @sql nvarchar(max)
declare @strWhere nvarchar(max)
declare @accountView nvarchar(500)
declare @projcetAreaWhere nvarchar(500)
declare @regionStr nvarchar(max)
declare @tradeStr nvarchar(max)
set @strWhere=' where 1=1 '
set @accountView = ''
set @projcetAreaWhere = ''
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @sql='select @regionStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=14 and 代码编码 in (
					select distinct substring(代码编码,1,2)+''0000'' from 系统_代码 where 代码类型编码=14 and 代码编码 in ('+@regionCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in ('+@tradeCode+')'
		set @sql='select @tradeStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=15 and 代码编码 in (
					select distinct substring(代码编码,1,2) from 系统_代码 where 代码类型编码=15 and 代码编码 in ('+@tradeCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@tradeStr nvarchar(max) output',@tradeStr output
	end
if(@controlType<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码 in ('+@controlType+')'
    end
if(@controlTypeGas<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码气 in ('+@controlTypeGas+')'
    end
if(@statType=1)
	begin
		set @accountView=' inner join 业务_污染源名录库_总站 as z on t.监测年=z.年度 and t.行政区代码=z.行政区代码 and t.法人代码=z.法人代码 and t.详细名称=z.企业名称 and t.企业类型=z.企业类型 '
	end
if(@projectArea<>'')
	begin
		if(@type=6)
		begin
			set @projcetAreaWhere = ' where 代码编码 in ('+@projectArea+')'
		end
		set @sql='select @regionStr=(select 代码名称+'','' from 系统_代码 where 代码类型编码=33 and 代码编码 in ('+@projectArea+') for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(len(@regionStr)>0) begin set @regionStr = substring(@regionStr,1,len(@regionStr)-1) end
if(len(@tradeStr)>0) begin set @tradeStr = substring(@tradeStr,1,len(@tradeStr)-1) end


--处理时间，将时间分为监测年和季度存到临时表中
EXEC GetQuarter @beginYear,@endYear,@beginQuarter,@endQuarter,'##tmp_Quarter'

--将符合条件的监测数据先存到临时表中
set @sql = 
	'select 监测年,行政区代码,行业代码,企业代码,监测点代码,监测项目编号,季度,废气排放量 into ##tmp_AllItem from
	(
		select t.* from
		(
			select a.监测年,a.行政区代码,a.详细名称,a.法人代码,a.行业代码,a.企业代码,b.监测点代码,a.污染源控制级别代码,a.污染源控制级别代码气,c.监测项目编号,d.季度,avg(d.废气排放量) as 废气排放量,b.企业类型
			from dbo.业务_企业基本信息 as a 
			inner join 业务_废气监测点信息 as b 
			on a.监测年=b.监测年 and a.企业代码=b.企业代码 and b.企业类型=1
			inner join 业务_企业监测点污染物信息 as c
			on b.企业代码=c.企业代码 and b.监测点代码=c.监测点代码 and b.监测年=c.监测年 and c.企业类型=b.企业类型
			inner join 业务_废气监测数据 as d 
			on d.废气排放量 is not null and c.企业代码=d.企业代码 and c.监测点代码=d.监测点代码
			and c.监测项目编号=d.监测项目编号 and c.监测年=d.监测年 and d.企业类型=c.企业类型
			inner join ##tmp_Quarter as q
			on d.监测年=q.监测年 and d.季度=q.季度
			group by a.监测年,a.行政区代码,a.行业代码,a.企业代码,a.详细名称,a.法人代码,b.监测点代码,a.污染源控制级别代码,a.污染源控制级别代码气,c.监测项目编号,d.季度,b.企业类型
		) as t' + @accountView + '
	) as t' + @strWhere
exec(@sql)

--按行业统计
if(@type=1)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,监测点数量,废气排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,b.监测点数量,b.废气排放量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,行业代码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废气排放量) as 废气排放量
			from
			(
				 select 监测年,季度,substring(行业代码,1,2) as 行业代码,企业代码,count(监测点代码) as 监测点数量,sum(废气排放量) as 废气排放量 
				 from ##tmp_AllItem
				 group by 监测年,季度,企业代码,substring(行业代码,1,2)
			) as a
			group by 监测年,季度,行业代码
		 ) as b 
		 on a.代码类型编码=15 and a.代码编码=b.行业代码
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',代码名称 as 行业,监测年,季度,企业数量,监测点数量 as 排口数量,废气排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,废气排放量','废气排放量','代码名称','avg','监测年,季度'
end
--按地区（省/市）统计
if(@type=4)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,监测点数量,废气排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,b.监测点数量,b.废气排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,行政区代码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废气排放量) as 废气排放量
			from
			(
				 select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,企业代码,count(监测点代码) as 监测点数量,sum(废气排放量) as 废气排放量 
				 from ##tmp_AllItem
				 group by 监测年,季度,企业代码,substring(行政区代码,1,2)+''0000''
			) as a
			group by 监测年,季度,行政区代码
			union all
			select 监测年,季度,行政区代码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废气排放量) as 废气排放量
			from
			(
				 select 监测年,季度,substring(行政区代码,1,4)+''00'' as 行政区代码,企业代码,count(监测点代码) as 监测点数量,sum(废气排放量) as 废气排放量 
				 from ##tmp_AllItem
				 group by 监测年,季度,企业代码,substring(行政区代码,1,4)+''00''
			) as a
			group by 监测年,季度,行政区代码
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,企业数量,监测点数量 as 排口数量,废气排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,废气排放量','废气排放量','代码名称','avg','监测年,季度'
end
--按地区（省）统计
if(@type=5)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,监测点数量,废气排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,b.监测点数量,b.废气排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,行政区代码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废气排放量) as 废气排放量
			from
			(
				 select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,企业代码,count(监测点代码) as 监测点数量,sum(废气排放量) as 废气排放量 
				 from ##tmp_AllItem
				 group by 监测年,季度,企业代码,substring(行政区代码,1,2)+''0000''
			) as a
			group by 监测年,季度,行政区代码
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,企业数量,监测点数量 as 排口数量,废气排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,废气排放量','废气排放量','代码名称','avg','监测年,季度'
end
--按专题区域统计
if(@type=6)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,监测点数量,废气排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,b.监测点数量,b.废气排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,区域类型编码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废气排放量) as 废气排放量
			from
			(
				 select b.区域类型编码,监测年,季度,行政区代码,企业代码,count(监测点代码) as 监测点数量,sum(废气排放量) as 废气排放量 
				 from ##tmp_AllItem as a
				 inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
				 group by 监测年,季度,企业代码,行政区代码,b.区域类型编码
			) as a
			group by 监测年,季度,区域类型编码
		 ) as b 
		 on a.代码类型编码=33 and a.ID=b.区域类型编码
	) as t' + @projcetAreaWhere
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,企业数量,监测点数量 as 排口数量,废气排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,废气排放量','废气排放量','代码名称','avg','监测年,季度'
end

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Cartogram_FSPFL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Cartogram_FSPFL]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-11-04
-- Description:	统计图-废水排放量
--==========================================
CREATE procedure [dbo].[Cartogram_FSPFL]
(
	@type int ,			--计算标准
	@beginYear int,		--起始年
	@endYear int,		--结束季度
	@beginQuarter int,  --起始季度
	@endQuarter int,	--结束季度
	@regionCode varchar(max),--地区编码
	@projectArea varchar(max),--专题区域编码
	@tradeCode varchar(max),--行业代码
	@whaterCode varchar(max),--受纳水体代码
	@controlType varchar(50),--废水控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int	--统计范围
)
as
begin

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

--设置查询条件
declare @sql nvarchar(max)
declare @strWhere nvarchar(max)
declare @accountView nvarchar(500)
declare @projcetAreaWhere nvarchar(500)
declare @regionStr nvarchar(max)
declare @tradeStr nvarchar(max)
set @strWhere=' where 1=1 '
set @accountView = ''
set @projcetAreaWhere = ''
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @sql='select @regionStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=14 and 代码编码 in (
					select distinct substring(代码编码,1,2)+''0000'' from 系统_代码 where 代码类型编码=14 and 代码编码 in ('+@regionCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(@tradeCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行业代码 in('+@tradeCode+')'
		set @strWhere = @strWhere + ' and 行业代码 in ('+@tradeCode+')'
		set @sql='select @tradeStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=15 and 代码编码 in (
					select distinct substring(代码编码,1,2) from 系统_代码 where 代码类型编码=15 and 代码编码 in ('+@tradeCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@tradeStr nvarchar(max) output',@tradeStr output
	end
if(@whaterCode<>'')
	 begin
		set @strWhere = @strWhere + ' and 受纳水体代码 in('+@whaterCode+')'
	 end
if(@controlType<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码 in ('+@controlType+')'
    end
if(@controlTypeGas<>'')
    begin
		set @strWhere = @strWhere + 'and 污染源控制级别代码气 in ('+@controlTypeGas+')'
    end
if(@statType=1)
	begin
		set @accountView=' inner join 业务_污染源名录库_总站 as z on t.监测年=z.年度 and t.行政区代码=z.行政区代码 and t.法人代码=z.法人代码 and t.详细名称=z.企业名称 and t.企业类型=z.企业类型 '
	end
if(@projectArea<>'')
	begin
		if(@type=6)
		begin
			set @projcetAreaWhere = ' where 代码编码 in ('+@projectArea+')'
		end
		set @sql='select @regionStr=(select 代码名称+'','' from 系统_代码 where 代码类型编码=33 and 代码编码 in ('+@projectArea+') for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(len(@regionStr)>0) begin set @regionStr = substring(@regionStr,1,len(@regionStr)-1) end
if(len(@tradeStr)>0) begin set @tradeStr = substring(@tradeStr,1,len(@tradeStr)-1) end

--处理时间，将时间分为监测年和季度存到临时表中
EXEC GetQuarter @beginYear,@endYear,@beginQuarter,@endQuarter,'##tmp_Quarter'

--将符合条件的监测数据先存到临时表中
set @sql = 
	'select 监测年,行政区代码,行业代码,企业代码,受纳水体代码,监测点代码,监测项目编号,季度,废水排放量 into ##tmp_AllItem from 
	(
		select t.* from
		(
			select a.监测年,a.行政区代码,a.详细名称,a.法人代码,a.行业代码,a.企业代码,b.受纳水体代码,b.监测点代码,a.污染源控制级别代码,a.污染源控制级别代码气,c.监测项目编号,d.季度,avg(d.废水排放量) as 废水排放量,b.企业类型
			from dbo.业务_企业基本信息 as a 
			inner join 业务_废水排口信息 as b 
			on a.监测年=b.监测年 and a.企业代码=b.企业代码 and b.企业类型=1
			inner join 业务_企业监测点污染物信息 as c
			on b.企业代码=c.企业代码 and b.监测点代码=c.监测点代码 and b.监测年=c.监测年 and c.企业类型=b.企业类型
			inner join 业务_废水监测数据 as d 
			on d.废水排放量 is not null and c.企业代码=d.企业代码 and c.监测点代码=d.监测点代码 
			and c.监测项目编号=d.监测项目编号 and c.监测年=d.监测年 and d.企业类型=c.企业类型
			inner join ##tmp_Quarter as q
			on d.监测年=q.监测年 and d.季度=q.季度
			group by a.监测年,a.行政区代码,a.详细名称,a.法人代码,a.行业代码,a.企业代码,b.受纳水体代码,b.监测点代码,a.污染源控制级别代码,a.污染源控制级别代码气,c.监测项目编号,d.季度,b.企业类型
		) as t' + @accountView + '
	) as t' + @strWhere
exec(@sql)

--按行业统计
if(@type=1)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,监测点数量,废水排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,b.监测点数量,b.废水排放量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,行业代码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废水排放量) as 废水排放量
			from
			(
				 select 监测年,季度,substring(行业代码,1,2) as 行业代码,企业代码,count(监测点代码) as 监测点数量,sum(废水排放量) as 废水排放量 
				 from ##tmp_AllItem
				 group by 监测年,季度,企业代码,substring(行业代码,1,2)
			) as a
			group by 监测年,季度,行业代码
		 ) as b 
		 on a.代码类型编码=15 and a.代码编码=b.行业代码
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',代码名称 as 行业,监测年,季度,企业数量,监测点数量 as 排口数量,废水排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,废水排放量','废水排放量','代码名称','avg','监测年,季度'
end
--按流域统计
if(@type=2)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,监测点数量,废水排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,b.监测点数量,b.废水排放量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,受纳水体代码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废水排放量) as 废水排放量
			from
			(
				 select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,企业代码,count(监测点代码) as 监测点数量,sum(废水排放量) as 废水排放量 
				 from ##tmp_AllItem
				 group by 监测年,季度,企业代码,substring(受纳水体代码,1,1)
			) as a
			group by 监测年,季度,受纳水体代码
		 ) as b 
		 on a.代码类型编码=20 and a.代码编码=b.受纳水体代码
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',@tradeStr as 行业,代码名称 as 流域,监测年,季度,企业数量,监测点数量 as 排口数量,废水排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,废水排放量','废水排放量','代码名称','avg','监测年,季度'
end
--按水系统计
if(@type=3)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,监测点数量,废水排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,b.监测点数量,b.废水排放量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,受纳水体代码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废水排放量) as 废水排放量
			from
			(
				 select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,企业代码,count(监测点代码) as 监测点数量,sum(废水排放量) as 废水排放量 
				 from ##tmp_AllItem
				 group by 监测年,季度,企业代码,substring(受纳水体代码,1,1)
			) as a
			group by 监测年,季度,受纳水体代码
		) as b 
		on a.代码类型编码=20 and a.代码编码=b.受纳水体代码
		union all
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,b.监测点数量,b.废水排放量 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,受纳水体代码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废水排放量) as 废水排放量
			from
			(
				 select 监测年,季度,substring(受纳水体代码,1,2)+''00000000'' as 受纳水体代码,企业代码,count(监测点代码) as 监测点数量,sum(废水排放量) as 废水排放量 
				 from ##tmp_AllItem
				 group by 监测年,季度,企业代码,substring(受纳水体代码,1,2)+''00000000''
			) as a
			group by 监测年,季度,受纳水体代码
		 ) as b 
		 on a.代码类型编码=16 and a.代码编码=b.受纳水体代码
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',@tradeStr as 行业,代码名称 as 流域,监测年,季度,企业数量,监测点数量 as 排口数量,废水排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,废水排放量','废水排放量','代码名称','avg','监测年,季度'
end
--按地区（省/市）统计
if(@type=4)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,监测点数量,废水排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,b.监测点数量,b.废水排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,行政区代码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废水排放量) as 废水排放量
			from
			(
				 select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,企业代码,count(监测点代码) as 监测点数量,sum(废水排放量) as 废水排放量 
				 from ##tmp_AllItem
				 group by 监测年,季度,企业代码,substring(行政区代码,1,2)+''0000''
			) as a
			group by 监测年,季度,行政区代码
			union all
			select 监测年,季度,行政区代码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废水排放量) as 废水排放量
			from
			(
				 select 监测年,季度,substring(行政区代码,1,4)+''00'' as 行政区代码,企业代码,count(监测点代码) as 监测点数量,sum(废水排放量) as 废水排放量 
				 from ##tmp_AllItem
				 group by 监测年,季度,企业代码,substring(行政区代码,1,4)+''00''
			) as a
			group by 监测年,季度,行政区代码
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,企业数量,监测点数量 as 排口数量,废水排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,废水排放量','废水排放量','代码名称','avg','监测年,季度'
end
--按地区（省）统计
if(@type=5)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,监测点数量,废水排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,b.监测点数量,b.废水排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,行政区代码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废水排放量) as 废水排放量
			from
			(
				 select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,企业代码,count(监测点代码) as 监测点数量,sum(废水排放量) as 废水排放量 
				 from ##tmp_AllItem
				 group by 监测年,季度,企业代码,substring(行政区代码,1,2)+''0000''
			) as a
			group by 监测年,季度,行政区代码
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,企业数量,监测点数量 as 排口数量,废水排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,废水排放量','废水排放量','代码名称','avg','监测年,季度'
end
--按专题区域统计
if(@type=6)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,企业数量,监测点数量,废水排放量 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.企业数量,b.监测点数量,b.废水排放量 from 
		系统_代码 as a
		inner join
		(
			select 监测年,季度,区域类型编码,count(企业代码) as 企业数量,sum(监测点数量) as 监测点数量,sum(废水排放量) as 废水排放量
			from
			(
				 select b.区域类型编码,监测年,季度,行政区代码,企业代码,count(监测点代码) as 监测点数量,sum(废水排放量) as 废水排放量 
				 from ##tmp_AllItem as a
				 inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
				 group by 监测年,季度,企业代码,行政区代码,b.区域类型编码
			) as a
			group by 监测年,季度,区域类型编码
		 ) as b 
		 on a.代码类型编码=33 and a.ID=b.区域类型编码
	) as t' + @projcetAreaWhere
	exec (@sql)
	select 代码名称 as '地区/专题区域',@tradeStr as 行业,监测年,季度,企业数量,监测点数量 as 排口数量,废水排放量 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,废水排放量','废水排放量','代码名称','avg','监测年,季度'
end

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Cartogram_WFH]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Cartogram_WFH]
GO

--==========================================
-- Author:		hanfei
-- Date:		2011-11-04
-- Description:	统计图-污水处理厂负荷
--==========================================
CREATE procedure [dbo].[Cartogram_WFH]
(
	@type int ,			--计算标准
	@beginYear int,		--起始年
	@endYear int,		--结束季度
	@beginQuarter int,  --起始季度
	@endQuarter int,	--结束季度
	@regionCode varchar(max),--地区编码
	@projectArea varchar(max),--专题区域编码
	@tradeCode varchar(max),--行业代码
	@whaterCode varchar(max),--受纳水体代码
	@controlType varchar(50),--废水控制级别
	@controlTypeGas varchar(50),--废气控制级别
	@statType int	--统计范围
)
as
begin

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

--设置查询条件
declare @sql nvarchar(max)
declare @strWhere nvarchar(max)
declare @accountView nvarchar(500)
declare @projcetAreaWhere nvarchar(500)
declare @regionStr nvarchar(max)
set @strWhere=' where 1=1 '
set @accountView = ''
set @projcetAreaWhere = ''
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
		set @sql='select @regionStr = (select 代码名称+'','' from 系统_代码 where 代码类型编码=14 and 代码编码 in (
					select distinct substring(代码编码,1,2)+''0000'' from 系统_代码 where 代码类型编码=14 and 代码编码 in ('+@regionCode+')
				) for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(@whaterCode<>'')
	 begin
		set @strWhere = @strWhere + ' and 受纳水体代码 in('+@whaterCode+')'
	 end
if(@statType=1)
	begin
		set @accountView=' inner join 业务_污染源名录库_总站 as z on t.监测年=z.年度 and t.行政区代码=z.行政区代码 and t.法人代码=z.法人代码 and t.详细名称=z.企业名称 and t.企业类型=z.企业类型 '
	end
if(@projectArea<>'')
	begin
		if(@type=6)
		begin
			set @projcetAreaWhere = ' where 代码编码 in ('+@projectArea+')'
		end
		set @sql='select @regionStr=(select 代码名称+'','' from 系统_代码 where 代码类型编码=33 and 代码编码 in ('+@projectArea+') for xml path(''''))'
		exec sp_executesql @sql,N'@regionStr nvarchar(max) output',@regionStr output
	end
if(len(@regionStr)>0) begin set @regionStr = substring(@regionStr,1,len(@regionStr)-1) end


--处理时间，将时间分为监测年和季度存到临时表中
EXEC GetQuarter @beginYear,@endYear,@beginQuarter,@endQuarter,'##tmp_Quarter'

--将符合条件的监测数据先存到临时表中
set @sql = 
	'select 监测年,行政区代码,企业代码,受纳水体代码,季度,设计处理能力,出口水量 into ##tmp_AllItem from 
	(
		select t.* from
		(
			select a.监测年,a.行政区代码,a.详细名称,a.法人代码,a.企业代码,a.受纳水体代码,c.季度,avg(a.设计处理能力) as 设计处理能力,avg(出口水量) as 出口水量,''2'' as 企业类型
			from dbo.业务_污水处理厂信息 as a 
			inner join 业务_污水处理厂污染物信息 as b
			on a.监测年=b.监测年 and a.企业代码=b.企业代码
			inner join 业务_污水处理厂监测数据 as c
			on b.监测年=c.监测年 and b.企业代码=c.企业代码 and b.监测项目编号=c.监测项目编号
			inner join ##tmp_Quarter as q
			on c.监测年=q.监测年 and c.季度=q.季度
			group by a.监测年,a.行政区代码,a.企业代码,a.详细名称,a.法人代码,a.受纳水体代码,c.季度
		) as t' + @accountView + '
	) as t' + @strWhere
exec(@sql)

--按流域统计
if(@type=2)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,污水处理厂数量,污水处理厂负荷 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.污水处理厂数量,case when isnull(出口水量,0) =0 then 0 else 出口水量/设计处理能力 end as 污水处理厂负荷 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,受纳水体代码,count(企业代码) as 污水处理厂数量,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力 from
			(	
				select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,企业代码,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力
				from ##tmp_AllItem
				group by 监测年,季度,企业代码,substring(受纳水体代码,1,1)
			) as t 
			group by 监测年,季度,受纳水体代码
		 ) as b 
		 on a.代码类型编码=20 and a.代码编码=b.受纳水体代码
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',代码名称 as 流域,监测年,季度,污水处理厂数量,污水处理厂负荷 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,污水处理厂负荷','污水处理厂负荷','代码名称','avg','监测年,季度'
end
--按水系统计
if(@type=3)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,污水处理厂数量,污水处理厂负荷 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.污水处理厂数量,case when isnull(出口水量,0) =0 then 0 else 出口水量/设计处理能力 end as 污水处理厂负荷 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,受纳水体代码,count(企业代码) as 污水处理厂数量,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力 from
			(	
				select 监测年,季度,substring(受纳水体代码,1,1) as 受纳水体代码,企业代码,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力
				from ##tmp_AllItem
				group by 监测年,季度,企业代码,substring(受纳水体代码,1,1)
			) as t 
			group by 监测年,季度,受纳水体代码
		) as b 
		on a.代码类型编码=20 and a.代码编码=b.受纳水体代码
		union all
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.污水处理厂数量,case when isnull(出口水量,0) =0 then 0 else 出口水量/设计处理能力 end as 污水处理厂负荷 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,受纳水体代码,count(企业代码) as 污水处理厂数量,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力 from
			(	
				select 监测年,季度,substring(受纳水体代码,1,2)+''00000000'' as 受纳水体代码,企业代码,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力
				from ##tmp_AllItem
				group by 监测年,季度,企业代码,substring(受纳水体代码,1,2)+''00000000''
			) as t 
			group by 监测年,季度,受纳水体代码
		) as b 
		on a.代码类型编码=16 and a.代码编码=b.受纳水体代码
	) as t'
	exec (@sql)
	select @regionStr as '地区/专题区域',代码名称 as 水系,监测年,季度,污水处理厂数量,污水处理厂负荷 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,污水处理厂负荷','污水处理厂负荷','代码名称','avg','监测年,季度'
end
--按地区（省/市）统计
if(@type=4)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,污水处理厂数量,污水处理厂负荷 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.污水处理厂数量,case when isnull(出口水量,0) =0 then 0 else 出口水量/设计处理能力 end as 污水处理厂负荷 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,行政区代码,count(企业代码) as 污水处理厂数量,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力 from
			(	
				select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,企业代码,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力
				from ##tmp_AllItem
				group by 监测年,季度,企业代码,substring(行政区代码,1,2)+''0000''
				union all
				select 监测年,季度,substring(行政区代码,1,4)+''00'' as 行政区代码,企业代码,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力
				from ##tmp_AllItem
				group by 监测年,季度,企业代码,substring(行政区代码,1,4)+''00''
			) as t 
			group by 监测年,季度,行政区代码
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',监测年,季度,污水处理厂数量,污水处理厂负荷 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,污水处理厂负荷','污水处理厂负荷','代码名称','avg','监测年,季度'
end
--按地区（省）统计
if(@type=5)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,污水处理厂数量,污水处理厂负荷 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.污水处理厂数量,case when isnull(出口水量,0) =0 then 0 else 出口水量/设计处理能力 end as 污水处理厂负荷 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,行政区代码,count(企业代码) as 污水处理厂数量,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力 from
			(	
				select 监测年,季度,substring(行政区代码,1,2)+''0000'' as 行政区代码,企业代码,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力
				from ##tmp_AllItem
				group by 监测年,季度,企业代码,substring(行政区代码,1,2)+''0000''
			) as t 
			group by 监测年,季度,行政区代码
		 ) as b 
		 on a.代码类型编码=14 and a.代码编码=b.行政区代码
	) as t'
	exec (@sql)
	select 代码名称 as '地区/专题区域',监测年,季度,污水处理厂数量,污水处理厂负荷 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,污水处理厂负荷','污水处理厂负荷','代码名称','avg','监测年,季度'
end
--按专题区域统计
if(@type=6)
begin
	set @sql=
	'select cast(监测年 as nvarchar)+''年''+(case when 季度=1 then ''第一季度'' when 季度=2 then ''第二季度'' when 季度=3 then ''第三季度'' when 季度=4 then ''第四季度'' end) as 时间,
		代码名称,代码编码,监测年,季度,污水处理厂数量,污水处理厂负荷 into ##tmp_AeverData from
	(
		select a.代码名称,a.代码编码,b.监测年,b.季度,b.污水处理厂数量,case when isnull(出口水量,0) =0 then 0 else 出口水量/设计处理能力 end as 污水处理厂负荷 from
		系统_代码 as a
		inner join
		(
			select 监测年,季度,区域类型编码,count(企业代码) as 污水处理厂数量,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力 from
			(	
				select b.区域类型编码,监测年,季度,行政区代码,企业代码,sum(出口水量) as 出口水量,sum(设计处理能力) as 设计处理能力
				from ##tmp_AllItem as a
				inner join 系统_专题区域代码 as b on a.行政区代码=b.行政区编码
				group by 监测年,季度,企业代码,行政区代码,b.区域类型编码
			) as t 
			group by 监测年,季度,区域类型编码
		 ) as b 
		 on a.代码类型编码=33 and a.ID=b.区域类型编码
	) as t' + @projcetAreaWhere
	exec (@sql)
	select 代码名称 as '地区/专题区域',监测年,季度,污水处理厂数量,污水处理厂负荷 
	from ##tmp_AeverData order by 监测年,季度,代码编码
	EXEC RowToColumn '##tmp_AeverData','时间','监测年,季度,时间,代码名称,污水处理厂负荷','污水处理厂负荷','代码名称','avg','监测年,季度'
end

if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end
if object_id('tempdb..##tmp_AllItem') is not null begin drop table ##tmp_AllItem end
if object_id('tempdb..##tmp_AeverData') is not null begin drop table ##tmp_AeverData end

end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Check_Enter]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Check_Enter]
GO


--==========================================
-- Author:		lizf
-- Date:		2011-11-08
-- Description:	用于数据上报污染源检查
-- @type: 1:污染源,2:监测点
-- @enterType:1:污染源,2:污水处理厂,3:固废处理厂
--==========================================
CREATE procedure [dbo].[Check_Enter]
	@beginTime nvarchar(50),
	@endTime nvarchar(50),
	@strWhere nvarchar(max),
	@type int,
	@controlType int,
	@controlTypeGas int,
	@outTable nvarchar(50)
as
begin

if object_id('tempdb..##tmp_FSEnter') is not null begin drop table ##tmp_FSEnter end
if object_id('tempdb..##tmp_FQEnter') is not null begin drop table ##tmp_FQEnter end
if object_id('tempdb..##tmp_FSItemData') is not null begin drop table ##tmp_FSItemData end
if object_id('tempdb..##tmp_FQItemData') is not null begin drop table ##tmp_FQItemData end
if object_id('tempdb..##tmp_AllEnter') is not null begin drop table ##tmp_AllEnter end
if object_id('tempdb..##tmp_AllData') is not null begin drop table ##tmp_AllData end
	
declare @sql nvarchar(max)
if(@controlType=1)
	begin
		--废水国控企业基本信息
		set @sql=
		   'select a.监测年,季度,企业代码,行政区代码,详细名称,法人代码,企业类型,''污染源'' as 企业属性,污染源控制级别代码,污染源控制级别代码气
			into ##tmp_FSEnter from V_污染源名录库 a
			inner join (select 监测年,季度 from ##tmp_Quarter) as q on a.监测年=q.监测年
			where 国控类型=1 and 基本信息企业代码 is not null'+@strWhere
		exec(@sql)
		--废水国控监测数据
		EXEC GetMonitorData 1,1,'##tmp_FSEnter','','##tmp_FSItemData'
	end
if(@controlTypeGas=1)
	begin
		--废气国控企业基本信息
		set @sql=
		   'select a.监测年,季度,企业代码,行政区代码,详细名称,法人代码,企业类型,''污染源'' as 企业属性,污染源控制级别代码,污染源控制级别代码气 
			into ##tmp_FQEnter from V_污染源名录库 a
			inner join (select 监测年,季度 from ##tmp_Quarter) as q on a.监测年=q.监测年
			where 国控类型=2 and 基本信息企业代码 is not null'+@strWhere
		exec(@sql)
		--废气国控监测数据
		EXEC GetMonitorData 1,2,'##tmp_FQEnter','','##tmp_FQItemData'
	end

--根据控制级别将数据整理到临时表
if(@controlType=1 and @controlTypeGas=1)
begin
	exec('select distinct a.监测年,季度,企业代码,行政区代码,详细名称,法人代码,企业类型,''污染源'' as 企业属性,污染源控制级别代码,污染源控制级别代码气
			into ##tmp_AllEnter from V_污染源名录库 a 
			inner join (select 监测年,季度 from ##tmp_Quarter) as q on a.监测年=q.监测年 
			where 1=1 '+@strWhere)
	exec('select * into ##tmp_AllData from (select * from ##tmp_FSItemData union select * from ##tmp_FQItemData) as t')
end
if(@controlType=1 and @controlTypeGas=0)
begin
	exec('select distinct a.监测年,季度,企业代码,行政区代码,详细名称,法人代码,企业类型,''污染源'' as 企业属性,污染源控制级别代码,污染源控制级别代码气
			into ##tmp_AllEnter from V_污染源名录库 a 
			inner join (select 监测年,季度 from ##tmp_Quarter) as q on a.监测年=q.监测年
            where 国控类型=1 '+@strWhere)
	exec('select * into ##tmp_AllData from ##tmp_FSItemData as t')
end
if(@controlType=0 and @controlTypeGas=1)
begin
	exec('select distinct a.监测年,季度,企业代码,行政区代码,详细名称,法人代码,企业类型,''污染源'' as 企业属性,污染源控制级别代码,污染源控制级别代码气
			into ##tmp_AllEnter from V_污染源名录库 a 
			inner join (select 监测年,季度 from ##tmp_Quarter) as q on a.监测年=q.监测年
            where 国控类型=2 '+@strWhere)
	exec('select * into ##tmp_AllData from ##tmp_FQItemData as t')
end

--污染源数据检查
if(@type=1)
begin
	set @sql=
		'select a.监测年,a.季度,a.企业代码,a.行政区代码,c1.代码名称 as 行政区,a.详细名称,a.企业类型,a.企业属性,a.法人代码,c2.代码名称+'',''+c3.代码名称 as 控制级别,c.未监测原因 into '+@outTable+' from 
		##tmp_AllEnter as a
		left join
		(
			select 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码,sum(是否监测) as 是否监测 from ##tmp_AllData
			where 应测监测项目编号 is not null
			group by 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码
		) as b
		on a.监测年=b.监测年 and a.季度=b.季度 and a.企业代码=b.企业代码
		left join 业务_企业未监测原因 as c
		on a.企业代码=c.企业代码 and a.企业类型=c.企业类型 and a.监测年=c.监测年 and a.季度=c.季度
		inner join 系统_代码 as c1
		on c1.代码类型编码=14 and a.行政区代码=c1.代码编码
		left join 系统_代码 as c2
		on c2.代码类型编码=6 and a.污染源控制级别代码=c2.代码编码
		left join 系统_代码 as c3
		on c3.代码类型编码=6 and a.污染源控制级别代码气=c3.代码编码
		where c.未监测原因 is null and isnull(b.是否监测,0)=0'
	exec(@sql)
end
--监测点数据检查
if(@type=2)
begin
	set @sql=
		'select a.监测年,a.季度,a.企业代码,a.行政区代码,c1.代码名称 as 行政区,a.详细名称,a.企业类型,a.企业属性,a.法人代码,c2.代码名称+'',''+c3.代码名称 as 控制级别,a.监测点代码,a.监测点名称,b.未监测原因 into '+@outTable+' from 
		(
			select 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码,监测点代码,监测点名称,污染源控制级别代码,污染源控制级别代码气 from ##tmp_AllData
			where 应测监测项目编号 is not null
			group by 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码,监测点代码,监测点名称,污染源控制级别代码,污染源控制级别代码气
			having sum(是否监测)=0
		) as a
		left join 业务_排口未监测原因 as b
		on a.企业代码=b.企业代码 and a.企业类型=b.企业类型 and a.监测年=b.监测年 and a.季度=b.季度 and a.监测点代码=b.监测点代码
		inner join 系统_代码 as c1
		on c1.代码类型编码=14 and a.行政区代码=c1.代码编码
		left join 系统_代码 as c2
		on c2.代码类型编码=6 and a.污染源控制级别代码=c2.代码编码
		left join 系统_代码 as c3
		on c3.代码类型编码=6 and a.污染源控制级别代码气=c3.代码编码
		where b.未监测原因 is null'
	exec(@sql)
end

if object_id('tempdb..##tmp_FSEnter') is not null begin drop table ##tmp_FSEnter end
if object_id('tempdb..##tmp_FQEnter') is not null begin drop table ##tmp_FQEnter end
if object_id('tempdb..##tmp_FSItemData') is not null begin drop table ##tmp_FSItemData end
if object_id('tempdb..##tmp_FQItemData') is not null begin drop table ##tmp_FQItemData end
if object_id('tempdb..##tmp_AllEnter') is not null begin drop table ##tmp_AllEnter end
if object_id('tempdb..##tmp_AllData') is not null begin drop table ##tmp_AllData end

end
go


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Check_Solid]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Check_Solid]
GO

--==========================================
-- Author:		lizf
-- Date:		2011-11-08
-- Description:	用于数据上报污染源检查__固废处理厂
-- @type: 1:污染源,2:监测点
--==========================================
CREATE procedure [dbo].[Check_Solid]
	@beginTime nvarchar(50),
	@endTime nvarchar(50),
	@strWhere nvarchar(max),
	@type int,
	@controlType int,
	@controlTypeGas int,
	@outTable nvarchar(50)
as
begin

if object_id('tempdb..##tmp_GFEnter') is not null begin drop table ##tmp_GFEnter end
if object_id('tempdb..##tmp_GFItemData') is not null begin drop table ##tmp_GFItemData end
if object_id('tempdb..##tmp_AllEnter') is not null begin drop table ##tmp_AllEnter end
if object_id('tempdb..##tmp_AllData') is not null begin drop table ##tmp_AllData end
	
declare @sql nvarchar(max)
--固废处理厂国控企业基本信息
set @sql=
   'select a.监测年,季度,企业代码,行政区代码,详细名称,法人代码,企业类型,''固废处理厂'' as 企业属性
	into ##tmp_GFEnter from V_固废处理厂名录库 a 
	inner join (select 监测年,季度 from ##tmp_Quarter) as q on a.监测年=q.监测年
	where 基本信息企业代码 is not null'+@strWhere
exec(@sql)
--固废处理厂国控监测数据
EXEC GetMonitorData 3,1,'##tmp_GFEnter','','##tmp_GFItemData'

--将数据整理到临时表
exec('select distinct a.监测年,季度,企业代码,行政区代码,详细名称,法人代码,企业类型,''固废处理厂'' as 企业属性
		into ##tmp_AllEnter from V_固废处理厂名录库 a 
		inner join (select 监测年,季度 from ##tmp_Quarter) as q on a.监测年=q.监测年 
        where 1=1 '+@strWhere)
exec('select * into ##tmp_AllData from ##tmp_GFItemData')


--污染源数据检查
if(@type=1)
begin
	set @sql=
		'select a.监测年,a.季度,a.企业代码,a.行政区代码,c1.代码名称 as 行政区,a.详细名称,a.企业类型,a.企业属性,a.法人代码,'''' as 控制级别,c.未监测原因 into '+@outTable+' from 
		##tmp_AllEnter as a
		left join
		(
			select 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码,sum(是否监测) as 是否监测 from ##tmp_AllData
			where 应测监测项目编号 is not null
			group by 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码
		) as b
		on a.监测年=b.监测年 and a.季度=b.季度 and a.企业代码=b.企业代码
		left join 业务_企业未监测原因 as c
		on a.企业代码=c.企业代码 and a.企业类型=c.企业类型 and a.监测年=c.监测年 and a.季度=c.季度
		inner join 系统_代码 as c1
		on c1.代码类型编码=14 and a.行政区代码=c1.代码编码
		where c.未监测原因 is null and isnull(b.是否监测,0)=0'
	exec(@sql)
end

--监测点数据检查
if(@type=2)
begin
	set @sql=
		'select a.监测年,a.季度,a.企业代码,a.行政区代码,c1.代码名称 as 行政区,a.详细名称,a.企业类型,a.企业属性,a.法人代码,'''' as 控制级别,a.监测点代码,a.监测点名称,b.未监测原因 into '+@outTable+' from 
		(
			select 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码,监测点代码,监测点名称 from ##tmp_AllData
			where 应测监测项目编号 is not null
			group by 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码,监测点代码,监测点名称
			having sum(是否监测)=0
		) as a
		left join 业务_排口未监测原因 as b
		on a.企业代码=b.企业代码 and a.企业类型=b.企业类型 and a.监测年=b.监测年 and a.季度=b.季度 and a.监测点代码=b.监测点代码
		inner join 系统_代码 as c1
		on c1.代码类型编码=14 and a.行政区代码=c1.代码编码
		where b.未监测原因 is null'
	exec(@sql)
end

if object_id('tempdb..##tmp_GFEnter') is not null begin drop table ##tmp_GFEnter end
if object_id('tempdb..##tmp_GFItemData') is not null begin drop table ##tmp_GFItemData end
if object_id('tempdb..##tmp_AllEnter') is not null begin drop table ##tmp_AllEnter end
if object_id('tempdb..##tmp_AllData') is not null begin drop table ##tmp_AllData end

end


GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Check_Water]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Check_Water]
GO


--==========================================
-- Author:		lizf
-- Date:		2011-11-08
-- Description:	用于数据上报污染源检查__污水处理厂
-- @type: 1:污染源,2:监测点
--==========================================
CREATE procedure [dbo].[Check_Water]
	@beginTime nvarchar(50),
	@endTime nvarchar(50),
	@strWhere nvarchar(max),
	@type int,
	@controlType int,
	@controlTypeGas int,
	@outTable nvarchar(50)
as
begin

if object_id('tempdb..##tmp_WSEnter') is not null begin drop table ##tmp_WSEnter end
if object_id('tempdb..##tmp_WSItemData') is not null begin drop table ##tmp_WSItemData end
if object_id('tempdb..##tmp_AllEnter') is not null begin drop table ##tmp_AllEnter end
if object_id('tempdb..##tmp_AllData') is not null begin drop table ##tmp_AllData end
	
declare @sql nvarchar(max)
--污水处理厂国控企业基本信息
set @sql=
   'select a.监测年,季度,企业代码,行政区代码,详细名称,法人代码,企业类型,''污水处理厂'' as 企业属性
	into ##tmp_WSEnter from V_污水处理厂名录库 a 
	inner join (select 监测年,季度 from ##tmp_Quarter) as q on a.监测年=q.监测年
	where 基本信息企业代码 is not null'+@strWhere
exec(@sql)
--污水处理厂国控监测数据
EXEC GetMonitorData 2,1,'##tmp_WSEnter','','##tmp_WSItemData'

--将数据整理到临时表
exec('select distinct a.监测年,季度,企业代码,行政区代码,详细名称,法人代码,企业类型,''污水处理厂'' as 企业属性
		into ##tmp_AllEnter from V_污水处理厂名录库 a 
		inner join (select 监测年,季度 from ##tmp_Quarter) as q on a.监测年=q.监测年
		where 1=1 '+@strWhere)
exec('select * into ##tmp_AllData from ##tmp_WSItemData')


--污染源数据检查
if(@type=1)
begin
	set @sql=
		'select a.监测年,a.季度,a.企业代码,a.行政区代码,c1.代码名称 as 行政区,a.详细名称,a.企业类型,a.企业属性,a.法人代码,'''' as 控制级别,c.未监测原因 into '+@outTable+' from 
		##tmp_AllEnter as a
		left join
		(
			select 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码,sum(是否监测) as 是否监测 from ##tmp_AllData
			where 应测监测项目编号 is not null
			group by 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码
		) as b
		on a.监测年=b.监测年 and a.季度=b.季度 and a.企业代码=b.企业代码
		left join 业务_企业未监测原因 as c
		on a.企业代码=c.企业代码 and a.企业类型=c.企业类型 and a.监测年=c.监测年 and a.季度=c.季度
		inner join 系统_代码 as c1
		on c1.代码类型编码=14 and a.行政区代码=c1.代码编码
		where c.未监测原因 is null and isnull(b.是否监测,0)=0'
	exec(@sql)
end

--监测点数据检查
if(@type=2)
begin
	set @sql=
		'select a.监测年,a.季度,a.企业代码,a.行政区代码,c1.代码名称 as 行政区,a.详细名称,a.企业类型,a.企业属性,a.法人代码,'''' as 控制级别,'''' as 监测点代码,'''' as 监测点名称,b.未监测原因 into '+@outTable+' from 
		(
			select 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码 from ##tmp_AllData
			where 应测监测项目编号 is not null
			group by 监测年,季度,企业代码,行政区代码,详细名称,企业类型,企业属性,法人代码
			having sum(是否监测)=0
		) as a
		left join 业务_排口未监测原因 as b
		on a.企业代码=b.企业代码 and a.企业类型=b.企业类型 and a.监测年=b.监测年 and a.季度=b.季度
		inner join 系统_代码 as c1
		on c1.代码类型编码=14 and a.行政区代码=c1.代码编码
		where b.未监测原因 is null'
	exec(@sql)
end

if object_id('tempdb..##tmp_WSEnter') is not null begin drop table ##tmp_WSEnter end
if object_id('tempdb..##tmp_WSItemData') is not null begin drop table ##tmp_WSItemData end
if object_id('tempdb..##tmp_AllEnter') is not null begin drop table ##tmp_AllEnter end
if object_id('tempdb..##tmp_AllData') is not null begin drop table ##tmp_AllData end

end

GO

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Check_UploadData]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Check_UploadData]
GO

--==========================================
-- Author:		lizf
-- Date:		2011-11-08
-- Description:	用于数据上报污染源检查
-- @type: 1:污染源,2:监测点
--==========================================
CREATE procedure [dbo].[Check_UploadData]
	@beginTime nvarchar(50),
	@endTime nvarchar(50),
	@type int,
	@regionCode	nvarchar(max),
	@controlType int,
	@controlTypeGas int,
	@enterType nvarchar(50)
as
begin

if object_id('tempdb..##tmp_EnterData') is not null begin drop table ##tmp_EnterData end
if object_id('tempdb..##tmp_SolidData') is not null begin drop table ##tmp_SolidData end
if object_id('tempdb..##tmp_WaterData') is not null begin drop table ##tmp_WaterData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

--设置查询条件
declare @strWhere nvarchar(max) --基本信息查询条件
set @strWhere = ''
if(@beginTime<>'')
	begin
		set @strWhere = @strWhere + ' and a.监测年>= '+cast(year(@beginTime) as nvarchar)
	end
if(@endTime<>'')
	begin
		set @strWhere = @strWhere + ' and a.监测年<= '+cast(year(@endTime) as nvarchar)
	end
if(@regionCode<>'')
	begin
		set @strWhere = @strWhere + ' and 行政区代码 in ('+@regionCode+')'
	end

--处理时间，将时间分为监测年和季度存到临时表中
declare @beginYear int
declare @endYear int
declare @beginQuarter int
declare @endQuarter int
set @beginYear = year(@beginTime)
set @endYear = year(@endTime)
set @beginQuarter = datepart(quarter,@beginTime)
set @endQuarter = datepart(quarter,@endTime)
EXEC GetQuarter @beginYear,@endYear,@beginQuarter,@endQuarter,'##tmp_Quarter'
	
--处理数据
EXEC Check_Enter @beginTime,@endTime,@strWhere,@type,@controlType,@controlTypeGas,'##tmp_EnterData'
EXEC Check_Solid @beginTime,@endTime,@strWhere,@type,@controlType,@controlTypeGas,'##tmp_SolidData'
EXEC Check_Water @beginTime,@endTime,@strWhere,@type,@controlType,@controlTypeGas,'##tmp_WaterData'

declare @sql nvarchar(max)
if(@type=1)
begin
	set @sql=
		'select * from 
		(
			select * from ##tmp_EnterData
			union all
			select * from ##tmp_SolidData
			union all
			select * from ##tmp_WaterData
		) as t
		where 企业类型 in ('+@enterType+') 
		order by 监测年,季度,行政区代码,详细名称'
end
if(@type=2)
begin
	set @sql=
		'select * from 
		(
			select * from ##tmp_EnterData
			union all
			select * from ##tmp_SolidData
			union all
			select * from ##tmp_WaterData
		) as t
		where 企业类型 in ('+@enterType+') 
		order by 监测年,季度,行政区代码,详细名称,监测点名称'
end
exec(@sql)

if object_id('tempdb..##tmp_EnterData') is not null begin drop table ##tmp_EnterData end
if object_id('tempdb..##tmp_SolidData') is not null begin drop table ##tmp_SolidData end
if object_id('tempdb..##tmp_WaterData') is not null begin drop table ##tmp_WaterData end
if object_id('tempdb..##tmp_Quarter') is not null begin drop table ##tmp_Quarter end

end

GO