-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[DataCompare] --新建存储过程
-- Add the parameters for the stored procedure here
@AreaCode varchar(50),--区域代码,参数都是输入参数
@AreaUnits varchar(50),--统计区域单位
@BeginTime nvarchar(10),--统计开始时间
@EndTime nvarchar(10)--统计结束时间
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--删除零时表数据
delete from temp_CompareDate
if @AreaUnits='CommunityCode'
begin
--社区代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CommunityCode' AreaUnits,a.CommunityCode from
(select StationID,avg(eday) StationAVG,CommunityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')!='' group by StationID,CommunityCode) as a,
(select AVG(eday) AreaAVG,CommunityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')!='' group by CommunityCode) as b
where a.CommunityCode=b.CommunityCode and a.StationAVG<b.AreaAVG
--乡镇代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'TownCode' AreaUnits,a.TownCode from
(select StationID,avg(eday) StationAVG,TownCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')!='' group by StationID,TownCode) as a,
(select AVG(eday) AreaAVG,TownCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')!='' group by TownCode) as b
where a.TownCode=b.TownCode and a.StationAVG<b.AreaAVG
--乡镇代码为空,县区代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CountyCode' AreaUnits,a.CountyCode from
(select StationID,avg(eday) StationAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')!='' group by StationID,CountyCode) as a,
(select AVG(eday) AreaAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')!='' group by CountyCode) as b
where a.CountyCode=b.CountyCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CityCode' AreaUnits,a.CityCode from
(select StationID,avg(eday) StationAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by StationID,CityCode) as a,
(select AVG(eday) AreaAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by CityCode) as b
where a.CityCode=b.CityCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码为空,省份代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'ProvinceCode' AreaUnits,a.ProvinceCode from
(select StationID,avg(eday) StationAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by StationID,ProvinceCode) as a,
(select AVG(eday) AreaAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by ProvinceCode) as b
where a.ProvinceCode=b.ProvinceCode and a.StationAVG<b.AreaAVG
--省份代码为空的不考虑
end
if @AreaUnits='TownCode'
begin
--乡镇代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'TownCode' AreaUnits,a.TownCode from
(select StationID,avg(eday) StationAVG,TownCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')!='' group by StationID,TownCode) as a,
(select AVG(eday) AreaAVG,TownCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')!='' group by TownCode) as b
where a.TownCode=b.TownCode and a.StationAVG<b.AreaAVG
--乡镇代码为空,县区代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CountyCode' AreaUnits,a.CountyCode from
(select StationID,avg(eday) StationAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')!='' group by StationID,CountyCode) as a,
(select AVG(eday) AreaAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')!='' group by CountyCode) as b
where a.CountyCode=b.CountyCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CityCode' AreaUnits,a.CityCode from
(select StationID,avg(eday) StationAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by StationID,CityCode) as a,
(select AVG(eday) AreaAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by CityCode) as b
where a.CityCode=b.CityCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码为空,省份代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'ProvinceCode' AreaUnits,a.ProvinceCode from
(select StationID,avg(eday) StationAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by StationID,ProvinceCode) as a,
(select AVG(eday) AreaAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by ProvinceCode) as b
where a.ProvinceCode=b.ProvinceCode and a.StationAVG<b.AreaAVG
--省份代码为空的不考虑
end
if @AreaUnits='CountyCode'
begin
--县区代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CountyCode' AreaUnits,a.CountyCode from
(select StationID,avg(eday) StationAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')!='' group by StationID,CountyCode) as a,
(select AVG(eday) AreaAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')!='' group by CountyCode) as b
where a.CountyCode=b.CountyCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CityCode' AreaUnits,a.CityCode from
(select StationID,avg(eday) StationAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by StationID,CityCode) as a,
(select AVG(eday) AreaAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by CityCode) as b
where a.CityCode=b.CityCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码为空,省份代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'ProvinceCode' AreaUnits,a.ProvinceCode from
(select StationID,avg(eday) StationAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by StationID,ProvinceCode) as a,
(select AVG(eday) AreaAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by ProvinceCode) as b
where a.ProvinceCode=b.ProvinceCode and a.StationAVG<b.AreaAVG
--省份代码为空的不考虑
end
if @AreaUnits='CityCode'
begin
--城市代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CityCode' AreaUnits,a.CityCode from
(select StationID,avg(eday) StationAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CityCode,'')!='' group by StationID,CityCode) as a,
(select AVG(eday) AreaAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CityCode,'')!='' group by CityCode) as b
where a.CityCode=b.CityCode and a.StationAVG<b.AreaAVG
--城市代码为空,省份代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'ProvinceCode' AreaUnits,a.ProvinceCode from
(select StationID,avg(eday) StationAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by StationID,ProvinceCode) as a,
(select AVG(eday) AreaAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by ProvinceCode) as b
where a.ProvinceCode=b.ProvinceCode and a.StationAVG<b.AreaAVG
--省份代码为空的不考虑
end
if @AreaUnits='ProvinceCode'
begin
--省份代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'ProvinceCode' AreaUnits,a.ProvinceCode from
(select StationID,avg(eday) StationAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(ProvinceCode,'')!='' group by StationID,ProvinceCode) as a,
(select AVG(eday) AreaAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(ProvinceCode,'')!='' group by ProvinceCode) as b
where a.ProvinceCode=b.ProvinceCode and a.StationAVG<b.AreaAVG
--省份代码为空的不考虑
end
END
GO
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[DataCompare] --新建存储过程
-- Add the parameters for the stored procedure here
@AreaCode varchar(50),--区域代码,参数都是输入参数
@AreaUnits varchar(50),--统计区域单位
@BeginTime nvarchar(10),--统计开始时间
@EndTime nvarchar(10)--统计结束时间
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
--删除零时表数据
delete from temp_CompareDate
if @AreaUnits='CommunityCode'
begin
--社区代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CommunityCode' AreaUnits,a.CommunityCode from
(select StationID,avg(eday) StationAVG,CommunityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')!='' group by StationID,CommunityCode) as a,
(select AVG(eday) AreaAVG,CommunityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')!='' group by CommunityCode) as b
where a.CommunityCode=b.CommunityCode and a.StationAVG<b.AreaAVG
--乡镇代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'TownCode' AreaUnits,a.TownCode from
(select StationID,avg(eday) StationAVG,TownCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')!='' group by StationID,TownCode) as a,
(select AVG(eday) AreaAVG,TownCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')!='' group by TownCode) as b
where a.TownCode=b.TownCode and a.StationAVG<b.AreaAVG
--乡镇代码为空,县区代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CountyCode' AreaUnits,a.CountyCode from
(select StationID,avg(eday) StationAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')!='' group by StationID,CountyCode) as a,
(select AVG(eday) AreaAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')!='' group by CountyCode) as b
where a.CountyCode=b.CountyCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CityCode' AreaUnits,a.CityCode from
(select StationID,avg(eday) StationAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by StationID,CityCode) as a,
(select AVG(eday) AreaAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by CityCode) as b
where a.CityCode=b.CityCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码为空,省份代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'ProvinceCode' AreaUnits,a.ProvinceCode from
(select StationID,avg(eday) StationAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by StationID,ProvinceCode) as a,
(select AVG(eday) AreaAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CommunityCode,'')='' AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by ProvinceCode) as b
where a.ProvinceCode=b.ProvinceCode and a.StationAVG<b.AreaAVG
--省份代码为空的不考虑
end
if @AreaUnits='TownCode'
begin
--乡镇代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'TownCode' AreaUnits,a.TownCode from
(select StationID,avg(eday) StationAVG,TownCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')!='' group by StationID,TownCode) as a,
(select AVG(eday) AreaAVG,TownCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')!='' group by TownCode) as b
where a.TownCode=b.TownCode and a.StationAVG<b.AreaAVG
--乡镇代码为空,县区代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CountyCode' AreaUnits,a.CountyCode from
(select StationID,avg(eday) StationAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')!='' group by StationID,CountyCode) as a,
(select AVG(eday) AreaAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')!='' group by CountyCode) as b
where a.CountyCode=b.CountyCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CityCode' AreaUnits,a.CityCode from
(select StationID,avg(eday) StationAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by StationID,CityCode) as a,
(select AVG(eday) AreaAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by CityCode) as b
where a.CityCode=b.CityCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码为空,省份代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'ProvinceCode' AreaUnits,a.ProvinceCode from
(select StationID,avg(eday) StationAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by StationID,ProvinceCode) as a,
(select AVG(eday) AreaAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(TownCode,'')='' AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by ProvinceCode) as b
where a.ProvinceCode=b.ProvinceCode and a.StationAVG<b.AreaAVG
--省份代码为空的不考虑
end
if @AreaUnits='CountyCode'
begin
--县区代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CountyCode' AreaUnits,a.CountyCode from
(select StationID,avg(eday) StationAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')!='' group by StationID,CountyCode) as a,
(select AVG(eday) AreaAVG,CountyCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')!='' group by CountyCode) as b
where a.CountyCode=b.CountyCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CityCode' AreaUnits,a.CityCode from
(select StationID,avg(eday) StationAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by StationID,CityCode) as a,
(select AVG(eday) AreaAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')!='' group by CityCode) as b
where a.CityCode=b.CityCode and a.StationAVG<b.AreaAVG
--县区代码为空,城市代码为空,省份代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'ProvinceCode' AreaUnits,a.ProvinceCode from
(select StationID,avg(eday) StationAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by StationID,ProvinceCode) as a,
(select AVG(eday) AreaAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CountyCode,'')='' AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by ProvinceCode) as b
where a.ProvinceCode=b.ProvinceCode and a.StationAVG<b.AreaAVG
--省份代码为空的不考虑
end
if @AreaUnits='CityCode'
begin
--城市代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'CityCode' AreaUnits,a.CityCode from
(select StationID,avg(eday) StationAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CityCode,'')!='' group by StationID,CityCode) as a,
(select AVG(eday) AreaAVG,CityCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CityCode,'')!='' group by CityCode) as b
where a.CityCode=b.CityCode and a.StationAVG<b.AreaAVG
--城市代码为空,省份代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'ProvinceCode' AreaUnits,a.ProvinceCode from
(select StationID,avg(eday) StationAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by StationID,ProvinceCode) as a,
(select AVG(eday) AreaAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(CityCode,'')='' AND ISNULL(ProvinceCode,'')!='' group by ProvinceCode) as b
where a.ProvinceCode=b.ProvinceCode and a.StationAVG<b.AreaAVG
--省份代码为空的不考虑
end
if @AreaUnits='ProvinceCode'
begin
--省份代码不为空
insert into temp_CompareDate( StationID, StationAVG, AreaAVG, AreaUnits, AreaCode)
select a.StationID,a.StationAVG,b.AreaAVG,'ProvinceCode' AreaUnits,a.ProvinceCode from
(select StationID,avg(eday) StationAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(ProvinceCode,'')!='' group by StationID,ProvinceCode) as a,
(select AVG(eday) AreaAVG,ProvinceCode from vStationRunBasicDatalast where createdate>@BeginTime and createdate<@EndTime
AND ISNULL(ProvinceCode,'')!='' group by ProvinceCode) as b
where a.ProvinceCode=b.ProvinceCode and a.StationAVG<b.AreaAVG
--省份代码为空的不考虑
end
END
GO