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伺服器上需要匯入Excel資料的必要條件SQLServer伺服器Excel
- SQL SERVER 條件語句的查詢SQLServer
- 關於使用exp按照條件匯出資料的問題(摘)
- SQL Server 查詢資料庫中所有表資料條數SQLServer資料庫
- PL/SQL 條件SQL
- SQL Server解惑——查詢條件IN中能否使用變數SQLServer變數
- 總結在SQL Server檢視管理中限制條件SQLServer
- SQL Server檢視管理中的四個限制條件SQLServer
- SQL Server 系統資料庫恢復SQLServer資料庫
- 在 SQL Server 2005 中配置資料庫郵件SQLServer資料庫
- SQL Server 資料庫設計規範SQLServer資料庫
- 【SQL】SQL中if條件的使用SQL
- SQL Server 2005 檢視資料庫表的大小 按照表大小排列SQLServer資料庫
- SQL Server 多表聯合查詢取最新一條資料SQLServer
- 利用SQL Server 2005資料庫郵件傳送電子郵件SQLServer資料庫
- SQL Server資料庫檔案不滿足擴充套件條件時不再自動擴充套件SQLServer資料庫套件
- SQL Server檢視管理中需要遵守的四個限制條件SQLServer
- SQL Server 更新統計資訊SQLServer
- 【開發篇sql】 條件和表示式(一) 資料型別SQL資料型別
- 特殊條件資料傳輸
- SQL中on條件與where條件的區別[轉]SQL
- SQL Server資料庫恢復,SQL Server資料恢復,SQL Server資料誤刪除恢復工具SQLRescueSQLServer資料庫資料恢復
- SQL多條件查詢SQL
- 進行SQL Server縱向擴充套件的必備條件KVSQLServer套件
- Sql Server系列:資料庫組成及系統資料庫SQLServer資料庫
- SQL Server資料庫安全SQLServer資料庫
- SQL server 修改表資料SQLServer
- SQL Server 資料庫映象SQLServer資料庫
- SQL Server 資料庫索引SQLServer資料庫索引
- 資料庫映象 (SQL Server)資料庫SQLServer
- sql 多組條資料取最新的一條資料SQL
- 【SQL Server】--SQL Server資料庫bak檔案還原SQLServer資料庫
- PL/SQL 條件控制語句SQL
- sql 查詢條件問題SQL
- 利用SQL Server發郵件 (轉)SQLServer