SQL server 实战篇(五) 查询数据库接收各设备的数据情况

本文章主要是针对数据库接收数据的情况,利用循环过程统计数据的传输率和最新上传的数据情况。

select * into #temptest from Data_DataDeskShow where  1 = 2
--FTIR/DOAS查询
declare @stationid int,@reporttime datetime,@belongdevices int
    set @stationid = 1
    set @reporttime = DATEADD(hour,12,'2018-06-07')
    while @stationid < 9
        begin
            set @belongdevices = 0
            while @belongdevices < 8
                begin   
                    insert into #temptest
                    select top 1 *  from Data_DataDeskShow where  stationID = @stationid and belongDevices = @belongdevices and reportTime > @reporttime order by reportTime desc
                    set @belongdevices += 1
                end
            set @stationid += 1
        end

--统计各子站的传输量

create table #temp_one(stationid varchar,belongDevices varchar,传输量 int)
insert into #temp_one select stationID,belongDevices,COUNT(*) '传输量' from Data_DataDeskShow 
where reportTime between DATEADD(hour,12,'2018-07-16') and DATEADD(hour,12,'2018-07-17') group by stationID,belongDevices order by stationID

select stationid,belongDevices,
转换 = case
when belongDevices = 0 then 传输量/10
when belongDevices = 1 then 传输量/5
when belongDevices = 5 then 传输量/120
when belongDevices = 6 then 传输量/60
when belongDevices = 7 then 传输量/30
else '无转换'
end
from #temp_one 

--重金属查询
declare @stationid int,@reporttime datetime,@belongdevices int
    set @stationid = 1007
    set @reporttime = DATEADD(hour,12,'2018-06-07')
    while @stationid < 1011
        begin
            set @belongdevices = 0
            while @belongdevices < 6
                BEGIN
            insert into #temptest
                    select top 1 *  from Data_DataDeskShow where  stationID = @stationid and belongDevices = @belongdevices and reportTime > @reporttime order by reportTime desc
                    set @belongdevices += 5
                END
            SET @stationid += 1 
        end

--甲烷非甲烷总烃
declare @stationid int,@reporttime datetime,@belongdevices int
    set @stationid = 1
    set @reporttime = DATEADD(hour,12,'2018-06-07')
    while @stationid < 5
    begin
        set @belongdevices = 7
        insert into #temptest
        select top 1 *  from Data_DataDeskShow where  stationID = @stationid and belongDevices = @belongdevices and reportTime > @reporttime order by reportTime desc
        set @stationid += 1
    end


select * from #temptest
drop TABLE #temptest

猜你喜欢

转载自blog.csdn.net/qq_39001049/article/details/81411458