简单存储过程的编写

-- ================================================
-- 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值