SQL Server 按照條件統計雨量資料
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<>'正常';
相關文章
- oracle按照表條件expdp匯出資料Oracle
- SQL Server統計資料庫中表大小SQLServer資料庫
- SQL Server 更新統計資訊SQLServer
- SQL Server 查詢資料庫中所有表資料條數SQLServer資料庫
- SQL Server解惑——查詢條件IN中能否使用變數SQLServer變數
- PL/SQL 條件SQL
- SQL Server資料庫恢復,SQL Server資料恢復,SQL Server資料誤刪除恢復工具SQLRescueSQLServer資料庫資料恢復
- 【SQL】SQL中if條件的使用SQL
- SQL server 修改表資料SQLServer
- 資料庫映象 (SQL Server)資料庫SQLServer
- SQL Server資料庫安全SQLServer資料庫
- 進行SQL Server縱向擴充套件的必備條件KVSQLServer套件
- [AlwaysOn] 建立SQL Server高可用性組T-SQL語法:先決條件和限制SQLServer
- Blazor使用sql server 資料庫BlazorSQLServer資料庫
- SQL Server資料庫巡檢SQLServer資料庫
- sql server 2005資料庫快照SQLServer資料庫
- SQL Server收縮資料庫SQLServer資料庫
- sql server 資料庫收縮SQLServer資料庫
- SQL Server資料庫遷移SQLServer資料庫
- SQL Server-資料型別SQLServer資料型別
- 管理SQL Server資料庫安全SQLServer資料庫
- 如何建立SQL Server分析系統資料收集組BSSQLServer
- 特殊條件資料傳輸
- PL/SQL 條件控制語句SQL
- sql 多組條資料取最新的一條資料SQL
- java 資料庫程式設計(一)JDBC連線Sql Server資料庫Java資料庫程式設計JDBCSQLServer
- SQL Server 2014 匯出資料字典SQLServer
- SQL Server 跨資料庫查詢SQLServer資料庫
- Logstash : 從 SQL Server 讀取資料SQLServer
- 資料庫映象 (SQL Server)操作模式資料庫SQLServer模式
- SQL Server 資料訪問策略:CLRMESQLServer
- 動態SQL-條件分頁SQL
- 通過新增條件優化SQL優化SQL
- sql 使用變數帶入in條件SQL變數
- Sql Server 資料庫學習-常用資料庫 物件SQLServer資料庫物件
- Serverless 解惑——函式計算如何訪問 SQL Server 資料庫Server函式SQL資料庫
- SQL Server 審計(Audit)SQLServer
- SQL Server 資料庫基本記錄(一)SQLServer資料庫
- 資料庫監視器(SQL Server Profilter)資料庫SQLServerFilter