1.整体存储
数据库 是fd的 这里
USE [****_fd]
GO /****** Object: StoredProcedure [dbo].[NGPhoneMonitoringpointData] Script Date: 2017/12/8 18:19:38 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO-- =============================================
-- Author: admin -- Create date: 2017/10/13 -- Description: 手机监控点数据 -- Test: NGPhoneMonitoringpointData '1','983','100040','2017-09-25 08:01','2017-09-27 10:01' -- Test: NGPhoneMonitoringpointData '2','983','100040','2017-10-02 10:01:00.000','2017-11-03 10:01:00.000' -- Test: NGPhoneMonitoringpointData '3','983','200001','2017-06-06 10:01:00.000','2017-10-01 10:01:00.000' -- Test: NGPhoneMonitoringpointData '4','983','200001','2017-06-06 10:01:00.000','2017-10-01 10:01:00.000' -- Test: NGPhoneMonitoringpointData '2','986','100003','2017-06-06 10:01:00.000','2017-10-01 10:01:00.000'-- Test: NGPhoneMonitoringpointData '5','985','100003','2017-10-02 10:01:00.000','2017-10-02 11:01:00.000'
-- Test: NGPhoneMonitoringpointData '6','985','20002', '2017-10-19 09:50:00.000','2017-10-19 10:00:00.000'-- Test: NGPhoneMonitoringpointData '6','985','100003','2017-10-02 10:01:00.000','2017-10-02 11:01:00.000'
-- ============================================= ALTER PROCEDURE [dbo].[NGPhoneMonitoringpointData] --判断那张表查数据 -- 5.扬尘或者是空气 6.小时数据 或者 日时间 -- 扬尘小时 = 1 扬尘日 =2 空气小时=3 空气日 =4 -- 判断要查询的表中的内容 -- 1.污染物编码 2.站点编码 3.开始时间 4.结束时间varchar(500),
@pulutioncode varchar(250),
@sitecode varchar(500), @begintime varchar(500), @endtime varchar(500) -- 需要的结果: -- update_date revised_monitor_valueAS BEGIN set nocount on -- 解决没有出现结果集的错误 declare @Timestart varchar(500) -- 输入的开始时间 declare @Timeend varchar(500) -- 拿到输入的开始时间 declare @beginHourTime varchar(500) -- 拿到输入的开始时间的小时部分 declare @Timestartadd1 varchar(500) -- 输入的开始时间 +1 declare @minute varchar(300) Select @Timestart = CONVERT(nvarchar(20),@begintime,120) Select @Timeend = CONVERT(nvarchar(20),@endtime,120)
declare @text varchar(255)
declare @value varchar(255) -- select * from t_dusty_monitor_factor_hour_data -- 扬尘小时 = 1 -- select convert(varchar(5),getdate(),114) if(@flag ='1') begin -- 小时数据拿到的是小时 -- 分析:当每次给小时部分加1 作为参数去判断条件 当没有值的时候value=0 create table #dataList (text varchar(800), value varchar(800),standard varchar(500)) select @beginHourTime = convert(varchar(13),@Timestart,121) --小时及以前 set @Timestart = @beginHourTime +':00:00' -- 拼接时间格式while(@Timestart < @Timeend)
begin select @value= [dbo].[AvgLimit](revised_monitor_value,0) from t_dusty_monitor_factor_hour_data where monitor_factor_code=@pulutioncode and site_code=@sitecode and convert(varchar(13),update_date,121)+':00:00' = @Timestart insert into #dataList values(@Timestart ,case when @value is NULL then 0 else @value end,'150') select @Timestart = CONVERT(nvarchar(20),dateadd(hh,1,@Timestart),120) -- 小时数据 +1 set @value = null; -- 清空数据 不然如果下面没有数据就会和上面一样 end select * ,'1' as type from #dataList order by text;drop table #dataList
end -- 扬尘日 =2 if(@flag ='2') begin -- 小时数据拿到的是小时 -- 分析:当每次给小时部分加1 作为参数去判断条件 当没有值的时候value=0 create table #dataList2 (text varchar(800), value varchar(800),standard varchar(500)) select @beginHourTime = convert(varchar(10),@Timestart,121) --小时及以前 set @Timestart = @beginHourTime +' 00:00:00' -- 拼接时间格式while(@Timestart < @Timeend)
begin select @value=[dbo].[AvgLimit](revised_monitor_value,0) from t_dusty_monitor_factor_day_data where monitor_factor_code=@pulutioncode and site_code=@sitecode and convert(varchar(10),update_date,121)+' 00:00:00' = @Timestart insert into #dataList2 values(@Timestart ,case when @value is NULL then 0 else @value end,'150') select @Timestart = CONVERT(nvarchar(20),dateadd(DD,1,@Timestart),120) -- 日时间加 1 set @value = null; -- 清空数据 end select * ,'1' as type from #dataList2 order by text; drop table #dataList2 end -- 空气小时=3 if(@flag ='3') begin -- 小时数据拿到的是小时 -- 分析:当每次给小时部分加1 作为参数去判断条件 当没有值的时候value=0 create table #dataList3 (text varchar(800), value varchar(800),standard varchar(500)) select @beginHourTime = convert(varchar(13),@Timestart,121) --小时及以前 set @Timestart = @beginHourTime +':00:00' -- 拼接时间格式while(@Timestart < @Timeend)
begin select @value= [dbo].[AvgLimit](revised_monitor_value,0) from t_air_monitor_factor_hour_data where monitor_factor_code=@pulutioncode and site_code=@sitecode and convert(varchar(13),update_date,121)+':00:00' = @Timestart insert into #dataList3 values(@Timestart ,case when @value is NULL then 0 else @value end,'150') select @Timestart = CONVERT(nvarchar(20),dateadd(hh,1,@Timestart),120) --小时加一小时 set @value = null; end select * ,'2' as type from #dataList3 order by text;drop table #dataList3
end-- 空气日 =4
if(@flag ='4') begin -- 小时数据拿到的是小时 -- 分析:当每次给小时部分加1 作为参数去判断条件 当没有值的时候value=0 create table #dataList4 (text varchar(800), value varchar(800),standard varchar(500)) select @beginHourTime = convert(varchar(10),@Timestart,121) --小时及以前 set @Timestart = @beginHourTime +' 00:00:00' -- 拼接时间格式while(@Timestart < @Timeend)
begin select @value=[dbo].[AvgLimit](revised_monitor_value,0) from t_air_monitor_factor_day_data where monitor_factor_code=@pulutioncode and site_code=@sitecode and convert(varchar(10),update_date,121)+' 00:00:00' = @Timestart insert into #dataList4 values(@Timestart ,case when @value is NULL then 0 else @value end,'150') select @Timestart = CONVERT(nvarchar(20),dateadd(DD,1,@Timestart),120) set @value = null; end select * ,'2' as type from #dataList4 order by text; drop table #dataList4 end-- 扬尘实时 type 1 扬尘 2 空气 3 国控
if(@flag ='5') begin -- 小时数据拿到的是小时 -- 分析:当每次给小时部分加1 作为参数去判断条件 当没有值的时候value=0 create table #dataList5 (text varchar(800), value varchar(800),standard varchar(500)) select @beginHourTime = convert(varchar(16),@Timestart,121) --小时及以前 set @Timestart = @beginHourTime +':00' -- 拼接时间格式while(@Timestart < @Timeend)
begin select @value=[dbo].[AvgLimit](revised_monitor_value,0) from t_dusty_monitor_factor_rtdata where monitor_factor_code=@pulutioncode and site_code=@sitecode and convert(varchar(16),update_date,121)+':00' = @Timestart insert into #dataList5 values(@Timestart ,case when @value is NULL then 0 else @value end,'150') select @Timestart = CONVERT(nvarchar(20),dateadd(MI,1,@Timestart),120) set @value = null; end select * ,'1' as type from #dataList5 order by text; drop table #dataList5 end-- 空气实时
if(@flag ='6') begin -- 小时数据拿到的是小时 -- 分析:当每次给小时部分加1 作为参数去判断条件 当没有值的时候value=0 create table #dataList6 (text varchar(800), value varchar(800),standard varchar(500)) select @beginHourTime = convert(varchar(16),@Timestart,121) --小时及以前 set @Timestart = @beginHourTime +':00' -- 拼接时间格式 while(@Timestart < @Timeend) begin select @value=[dbo].[AvgLimit](revised_monitor_value,0) from t_air_monitor_factor_rtdata where monitor_factor_code=@pulutioncode and site_code=@sitecode and convert(varchar(16),update_date,121)+':00' = @Timestart insert into #dataList6 values(@Timestart ,case when @value is NULL then 0 else @value end,'150') select @Timestart = CONVERT(nvarchar(20),dateadd(MI,1,@Timestart),120) -- MI 表示分钟 set @value = null; end select * ,'2' as type from #dataList6 order by text; drop table #dataList6 end END2.测试
2.1 小时 效果
2.2 日数据
2.3 分钟数据
2.单独 拿出小时数据 存储 这样 清晰
2.1效果
2.存储
USE [***_fd]
GO /****** Object: StoredProcedure [dbo].[NG_Phone_GK_hourData] Script Date: 2017/12/8 18:16:15 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- Test: NG_Phone_GK_hourData '985','2017-11-03 10:01:00.000','2017-11-04 10:01:00.000' -- ============================================= ALTER PROCEDURE [dbo].[NG_Phone_GK_hourData] -- 参数 @pulutioncode varchar(50), -- 污染物类型 如 985 986 @begintime varchar(500), @endtime varchar(500) AS BEGIN -- 变量 SET NOCOUNT ON; declare @Timestart varchar(500) -- 输入的开始时间 declare @Timeend varchar(500) -- 拿到输入的开始时间 declare @beginHourTime varchar(500) -- 拿到输入的开始时间的小时部分 declare @endHourTime varchar(500) declare @text varchar(255) declare @value varchar(255) -- sql begin -- 小时数据拿到的是小时 -- 分析:当每次给小时部分加1 作为参数去判断条件 当没有值的时候value=0 create table #dataList (text varchar(800), value varchar(800),standard varchar(500)) select @beginHourTime = convert(varchar(13),@begintime,121) --小时及以前 set @Timestart = @beginHourTime +':00:00' -- 拼接时间格式select @endHourTime = convert(varchar(13),@endtime,121) --小时及以前
set @Timeend = @endHourTime +':00:00' -- 拼接时间格式while(@Timestart < @Timeend)
begin select @value= [dbo].[AvgLimit](pm25,0) from t_air_monitor_factor_hour_data_gk where convert(varchar(13),update_date,121)+':00:00' = @Timestart insert into #dataList values(@Timestart ,case when @value is NULL then 0 else @value end,'150') select @Timestart = CONVERT(nvarchar(20),dateadd(hh,1,@Timestart),120) -- 小时数据 +1 set @value = null; -- 清空数据 不然如果下面没有数据就会和上面一样 end select * ,'3' as type from #dataList order by text;drop table #dataList
end END