博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
61.查询历史数据
阅读量:6877 次
发布时间:2019-06-26

本文共 8883 字,大约阅读时间需要 29 分钟。

hot3.png

 

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_value

    

AS
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
        
END

2.测试

2.1 小时 效果

183408_feqL_3015807.png

2.2  日数据

183541_Xf7r_3015807.png

2.3 分钟数据

184020_gpwq_3015807.png

2.单独 拿出小时数据 存储  这样   清晰

2.1效果

184810_olMz_3015807.png

 

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
 

转载于:https://my.oschina.net/springMVCAndspring/blog/1587066

你可能感兴趣的文章
mysql之innodb的mvcc多版本控制
查看>>
使用 LogStash 归集日志
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
德国博世百年风雨启示录(下):实业强国
查看>>
(整理)用Elixir做一个多人扑克游戏 4
查看>>
关于架构
查看>>
The application’s PagerAdapter changed the adapter’s contents without calling PagerA
查看>>
qcom 跨平台的串口调试工具 PKGBUILD
查看>>
Delphi 时间格式化,动态显示时间,显示最新时间
查看>>
在JAVA中将NEW一分为2,分步进行[反射机制产生类]
查看>>
Java多态性的两个特殊情况
查看>>
我的友情链接
查看>>
怎么改变Win7登陆背景图片
查看>>
虚拟带库和物理带库比较
查看>>
AD委派加域权限
查看>>
在Delphi 7中使用加密的SQLite
查看>>
wordpress 无用的RSS Feed Cache
查看>>
Linux Vi编辑器的基本使用方法
查看>>
HTTP头域列表与解释 之 response篇
查看>>