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<>'正常';



 

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
相关文章
|
7天前
|
SQL 人工智能 算法
【SQL server】玩转SQL server数据库:第二章 关系数据库
【SQL server】玩转SQL server数据库:第二章 关系数据库
46 10
|
19天前
|
SQL 存储 关系型数据库
一文搞懂SQL优化——如何高效添加数据
**SQL优化关键点:** 1. **批量插入**提高效率,一次性建议不超过500条。 2. **手动事务**减少开销,多条插入语句用一个事务。 3. **主键顺序插入**避免页分裂,提升性能。 4. **使用`LOAD DATA INFILE`**大批量导入快速。 5. **避免主键乱序**,减少不必要的磁盘操作。 6. **选择合适主键类型**,避免UUID或长主键导致的性能问题。 7. **避免主键修改**,保持索引稳定。 这些技巧能优化数据库操作,提升系统性能。
214 4
一文搞懂SQL优化——如何高效添加数据
|
1月前
|
SQL 数据库 数据安全/隐私保护
Sql Server数据库Sa密码如何修改
Sql Server数据库Sa密码如何修改
|
17天前
|
SQL 关系型数据库 MySQL
mysql一条sql查询出多个统计结果
mysql一条sql查询出多个统计结果
13 0
|
1月前
|
SQL 数据可视化 数据处理
使用SQL和Python处理Excel文件数据
使用SQL和Python处理Excel文件数据
51 0
|
17天前
|
SQL
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
启动mysq异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案
15 0
|
7天前
|
SQL 算法 数据库
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
【SQL server】玩转SQL server数据库:第三章 关系数据库标准语言SQL(二)数据查询
64 6
|
1月前
|
SQL
现有用户成就统计需求,每个用户有多个成就,某一个成就会被多人拥有,写出数据表设计方案,用一条sql查出每个成就(B.ach_name)下的男生(sex=0)和女生(sex=1)分别有多少?
现有用户成就统计需求,每个用户有多个成就,某一个成就会被多人拥有,写出数据表设计方案,用一条sql查出每个成就(B.ach_name)下的男生(sex=0)和女生(sex=1)分别有多少?
41 0
|
7天前
|
SQL 存储 数据挖掘
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
服务器数据恢复环境: 一台安装windows server操作系统的服务器。一组由8块硬盘组建的RAID5,划分LUN供这台服务器使用。 在windows服务器内装有SqlServer数据库。存储空间LUN划分了两个逻辑分区。 服务器故障&初检: 由于未知原因,Sql Server数据库文件丢失,丢失数据涉及到3个库,表的数量有3000左右。数据库文件丢失原因还没有查清楚,也不能确定数据存储位置。 数据库文件丢失后服务器仍处于开机状态,所幸没有大量数据写入。 将raid5中所有磁盘编号后取出,经过硬件工程师检测,没有发现明显的硬件故障。以只读方式将所有磁盘进行扇区级的全盘镜像,镜像完成后将所
数据库数据恢复—RAID5上层Sql Server数据库数据恢复案例
|
11天前
|
SQL 安全 Java
SQL server 2017安装教程
SQL server 2017安装教程
14 1