SQL Server 按照条件统计雨量数据

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
简介: SQL中获取当前时间的小时数: select ltrim(datepart(hh,getdate()));  问题条件:如果当前时间在8时之前,则取昨日8时后的累积雨量,如果在8日之后,则从今日8时之后取数据,SQL语句如下: select t.

SQL中获取当前时间的小时数:

select ltrim(datepart(hh,getdate()));


 

问题条件:

如果当前时间在8时之前,则取昨日8时后的累积雨量,如果在8日之后,则从今日8时之后取数据,SQL语句如下:

 

select t.STNM,t1.DRP,t.LTTD,t.LGTD,t1.STCD from
(
    select STNM,LTTD,LGTD,STCD from ST_STBPRP_B
) t
,
(
      select distinct(STCD) as STCD,ttt.DRP as DRP from ST_PPTN_R tt
      left join 
      (
           select STCD as STCD2,sum(DRP) as DRP from ST_PPTN_R where
           TM>=
            (
                case when ltrim(datepart(hh,getdate()))>=8 then convert(varchar(10),getdate(),120)+' 08:00' else convert(varchar(10),getdate()-1,120)+' 08:00' end
            ) 
            group by STCD
      ) ttt 
      on tt.STCD=ttt.STCD2 
) t1 
where t.STCD=t1.STCD;

 

select t.STNM,t1.Z,t.LTTD,t.LGTD,t1.STCD from
				(
    					select STNM,LTTD,LGTD,STCD,ADDVCD,STTP from ST_STBPRP_B 
				)t
				,
				(
     					select distinct(STCD) as STCD,t3.Z from 
     					(
         					select STCD,TM,Z from ST_RIVER_R
         					union 
         					select STCD,TM,RZ as Z from ST_RSVR_R
     					) t2 
     					left join
     					(
       						  select tt.STCD as STCD2, avg(Z) as Z from 
         					 (
            						 select STCD,TM,Z from ST_RIVER_R
             						 union 
             						 select STCD,TM,RZ as Z from ST_RSVR_R
         					) tt
         					where  tt.TM>=
                                                (
                        				case when ltrim(datepart(hh,getdate()))>=8 then convert(varchar(10),getdate(),120)+' 08:00' else convert(varchar(10),getdate()-1,120)+' 08:00' end  
                                                )
                                                group by tt.STCD
      					) t3
      					on t2.STCD = t3.STCD2
				) t1
				where  t.STCD = t1.STCD and t.STTP not in ('pp') and t.ADDVCD like '3410%'



--所有雨量站点
select d.STNM,
(
 	case when d.STCD=e.STCD then e.DRP else 0 end
) as DRP,
d.LTTD,d.LGTD,d.STCD
from
(
	select t.STNM,t.LTTD,t.LGTD,t.STCD from ST_STBPRP_B t where STTP='PP' or STCD like '_______4'
) d
left join
(
	select t.STNM,ISNULL(t1.DRP,0) DRP,t.LTTD,t.LGTD,t1.STCD from
	(
   		 select STNM,LTTD,LGTD,STCD from ST_STBPRP_B where STTP='PP' or STCD like '_______4'
	) t
	,
	(
      		select distinct(STCD) as STCD,ttt.DRP as DRP from ST_PPTN_R tt
      		left join 
      		(
			select STCD as STCD2,sum(DRP) as DRP from ST_PPTN_R where
			TM>=
			(
   				case when ltrim(datepart(hh,getdate()))>=8 then convert(varchar(10),getdate(),120)+' 08:00' else convert(varchar(10),getdate()-1,120)+' 08:00' end
			) 
			group by STCD
      		) ttt 
      		on tt.STCD=ttt.STCD2 
	) t1 
	where t.STCD=t1.STCD
) e
on d.STCD=e.STCD;

--所有水位站


select d.STNM,
(
  	case when d.STCD=e.STCD then e.Z else 0 end
) as Z,
d.LTTD,d.LGTD,d.STCD
from
(
 	select t.STNM,t.LTTD,t.LGTD,t.STCD from ST_STBPRP_B t where STTP<>'PP'
) d
left join
(
 	select t.STNM,t1.Z,t.LTTD,t.LGTD,t1.STCD from
 	(
      		select STNM,LTTD,LGTD,STCD from ST_STBPRP_B 
 	)t
 	,
 	(
    	   	select * from 
  	(  
   		select STCD,TM,Z from ST_RIVER_R
    		union 
      	         select STCD,TM,RZ as Z from ST_RSVR_R  
  	) t
  	where t.TM in   
 	(  
     	 	select MAX(TM) from 
      		(
            		select STCD,TM,Z from ST_RIVER_R
       			union 
       			select STCD,TM,RZ as Z from ST_RSVR_R  
      		) t1
     		where t.STCD=t1.STCD  
 	 )  
 ) t1
 where  t.STCD = t1.STCD
) e
on d.STCD=e.STCD;


---------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

--报警水库数据
create view v_SK_WarningInfo
as
select zdt8 as s,tmnow as slasttime,t.STCD as 水位站编码,STNM as 水位站名称,STLC as 所属县,LGTD,LTTD,ShowLevel,WRZ,GRZ,tt.STTP,tt.RVNM,t.Msg
from
(
     select STCD,STNM,LGTD,LTTD,STLC,zdt8,tmnow,ShowLevel,IsNull(WRZ,0) as WRZ,IsNull(GRZ,0) as GRZ,case when WRZ-zdt8=0 then '达警戒' when zdt8 between WRZ and GRZ then '超警戒:'+cast((zdt8-WRZ) as varchar(10))  when zdt8>GRZ then '超保证:'+cast((zdt8-GRZ) as varchar(10)) else '正常' end as Msg from ST_RIVER_D
     union
    select STCD,STNM,LGTD,LTTD,STLC,zdt8,tmnow,ShowLevel,IsNull(FSLTDZ,0) as WRZ,0 as GRZ,case when FSLTDZ-zdt8=0 then '达汛限'  when zdt8>FSLTDZ then '超汛限:'+cast((zdt8-FSLTDZ) as varchar(10)) else '正常' end as Msg from ST_RSVR_D
) t,
(
    select STCD,STTP,RVNM from ST_STBPRP_B where STTP='RR'
) tt
where t.STCD=tt.STCD and Msg<>'正常';


--报警河道数据
create view v_HD_WarningInfo
as
select zdt8 as s,tmnow as slasttime,t.STCD as 水位站编码,STNM as 水位站名称,STLC as 所属县,LGTD,LTTD,ShowLevel,WRZ,GRZ,tt.STTP,tt.RVNM,t.Msg
from
(
     select STCD,STNM,LGTD,LTTD,STLC,zdt8,tmnow,ShowLevel,IsNull(WRZ,0) as WRZ,IsNull(GRZ,0) as GRZ,case when WRZ-zdt8=0 then '达警戒' when zdt8 between WRZ and GRZ then '超警戒:'+cast((zdt8-WRZ) as varchar(10))  when zdt8>GRZ then '超保证:'+cast((zdt8-GRZ) as varchar(10)) else '正常' end as Msg from ST_RIVER_D
     union
    select STCD,STNM,LGTD,LTTD,STLC,zdt8,tmnow,ShowLevel,IsNull(FSLTDZ,0) as WRZ,0 as GRZ,case when FSLTDZ-zdt8=0 then '达汛限'  when zdt8>FSLTDZ then '超汛限:'+cast((zdt8-FSLTDZ) as varchar(10)) else '正常' end as Msg from ST_RSVR_D
) t,
(
    select STCD,STTP,RVNM from ST_STBPRP_B where STTP='ZZ'
) tt
where t.STCD=tt.STCD and Msg<>'正常';



 

相关文章
|
3月前
|
SQL 人工智能 JSON
Flink 2.1 SQL:解锁实时数据与AI集成,实现可扩展流处理
简介:本文整理自阿里云高级技术专家李麟在Flink Forward Asia 2025新加坡站的分享,介绍了Flink 2.1 SQL在实时数据处理与AI融合方面的关键进展,包括AI函数集成、Join优化及未来发展方向,助力构建高效实时AI管道。
765 43
|
3月前
|
SQL 人工智能 JSON
Flink 2.1 SQL:解锁实时数据与AI集成,实现可扩展流处理
本文整理自阿里云的高级技术专家、Apache Flink PMC 成员李麟老师在 Flink Forward Asia 2025 新加坡[1]站 —— 实时 AI 专场中的分享。将带来关于 Flink 2.1 版本中 SQL 在实时数据处理和 AI 方面进展的话题。
276 0
Flink 2.1 SQL:解锁实时数据与AI集成,实现可扩展流处理
|
4月前
|
SQL
SQL如何只让特定列中只显示一行数据
SQL如何只让特定列中只显示一行数据
|
8月前
|
SQL 自然语言处理 数据库
【Azure Developer】分享两段Python代码处理表格(CSV格式)数据 : 根据每列的内容生成SQL语句
本文介绍了使用Python Pandas处理数据收集任务中格式不统一的问题。针对两种情况:服务名对应多人拥有状态(1/0表示),以及服务名与人名重复列的情况,分别采用双层for循环和字典数据结构实现数据转换,最终生成Name对应的Services列表(逗号分隔)。此方法高效解决大量数据的人工处理难题,减少错误并提升效率。文中附带代码示例及执行结果截图,便于理解和实践。
224 4
|
4月前
|
SQL
SQL中如何删除指定查询出来的数据
SQL中如何删除指定查询出来的数据
|
4月前
|
SQL 关系型数据库 MySQL
SQL如何对不同表的数据进行更新
本文介绍了如何将表A的Col1数据更新到表B的Col1中,分别提供了Microsoft SQL和MySQL的实现方法,并探讨了多表合并后更新的优化方式,如使用MERGE语句提升效率。适用于数据库数据同步与批量更新场景。
|
5月前
|
SQL DataWorks 数据管理
SQL血缘分析实战!数据人必会的3大救命场景
1. 开源工具:Apache Atlas(元数据管理)、Spline(血缘追踪) 2. 企业级方案:阿里DataWorks血缘分析、腾讯云CDW血缘引擎 3. 自研技巧:在ETL脚本中植入版本水印,用注释记录业务逻辑变更 📌 重点总结:
|
6月前
|
SQL 数据挖掘 关系型数据库
【SQL 周周练】一千条数据需要做一天,怎么用 SQL 处理电表数据(如何动态构造自然月)
题目来自于某位发帖人在某 Excel 论坛的求助,他需要将电表缴费数据按照缴费区间拆开后再按月份汇总。当时用手工处理数据,自称一千条数据就需要处理一天。我将这个问题转化为 SQL 题目。
235 12
|
6月前
|
SQL 数据采集 资源调度
【SQL 周周练】爬取短视频发现数据缺失,如何用 SQL 填充
爬虫爬取抖音和快手的短视频数据时,如果遇到数据缺失的情况,如何使用 SQL 语句完成数据的补全。
161 5
|
9月前
|
SQL 关系型数据库 MySQL
基于SQL Server / MySQL进行百万条数据过滤优化方案
对百万级别数据进行高效过滤查询,需要综合使用索引、查询优化、表分区、统计信息和视图等技术手段。通过合理的数据库设计和查询优化,可以显著提升查询性能,确保系统的高效稳定运行。
418 9