水利防汛之水库和河道预警SQL语句

简介: SQL Server版本:create view v_HD_WarningInfoasSELECT t .

SQL Server版本:

create view v_HD_WarningInfo
as
SELECT     t .zdt8 , t .tmnow , t .STCD , t .STNM, t .STLC, t .LGTD, t .LTTD, t .ShowLevel, t .WRZ, t .GRZ, tt.STTP, tt.RVNM,
                       t .Msg
FROM         (SELECT     t1.STCD, t1.STNM, t1.LGTD, t1.LTTD, t1.STLC, t1.zdt8, t1.tmnow, t1.ShowLevel, t2.WRZ, t2.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          (SELECT     STCD, STNM, LGTD, LTTD, STLC, Z AS zdt8, tmnow, ShowLevel
                                               FROM          ST_RIVER_D) t1 LEFT JOIN
                                                  (SELECT     STCD, ISNULL(WRZ, 0) AS WRZ, ISNULL(GRZ, 0) AS GRZ
                                                    FROM          ST_RVFCCH_B) t2 ON t1.STCD = t2.STCD
                       UNION ALL
                       SELECT     t1.STCD, t1.STNM, t1.LGTD, t1.LTTD, t1.STLC, t1.zdt8, t1.tmnow, t1.ShowLevel, t2.WRZ, t2.GRZ, 
                                             CASE WHEN WRZ - zdt8 = 0 THEN '达汛限' WHEN zdt8 > WRZ THEN '超汛限:' + CAST((zdt8 - WRZ) AS varchar(10)) ELSE '正常' END AS Msg
                       FROM         (SELECT     STCD, STNM, LGTD, LTTD, STLC, RZ AS zdt8, tmnow, ShowLevel
                                              FROM          ST_RSVR_D) t1 LEFT JOIN
                                                 (SELECT     STCD, BGMD, EDMD, ISNULL(FSLTDZ, 0) AS WRZ, 0 AS GRZ
                                                   FROM          ST_RSVRFSR_B) t2 ON t1.STCD = t2.STCD AND t1.tmnow BETWEEN CONVERT(varchar, YEAR(GETDATE()), 4) + '-' + SUBSTRING(t2.BGMD, 1, 
                                             2) + '-' + SUBSTRING(t2.BGMD, 3, 2) AND CONVERT(varchar, YEAR(GETDATE()), 4) + '-' + SUBSTRING(t2.EDMD, 1, 2) + '-' + SUBSTRING(t2.EDMD, 3, 2)) 
                      AS t INNER JOIN
                          (SELECT     STCD, STTP, RVNM
                            FROM          dbo.ST_STBPRP_B
                            WHERE      (STTP = 'RR')) AS tt ON t .STCD = tt.STCD
WHERE     (t .Msg <> '正常') AND t .tmnow >= dateadd([day], - 3, GETDATE());




-------------------------------------------------------------------------------------------------
create view v_SK_WarningInfo
as
SELECT     t .zdt8, t .tmnow , t .STCD , t .STNM , t .STLC , t .LGTD, t .LTTD, t .ShowLevel, t .WRZ, t .GRZ, tt.STTP, tt.RVNM,
                       t .Msg
FROM         (SELECT     t1.STCD, t1.STNM, t1.LGTD, t1.LTTD, t1.STLC, t1.zdt8, t1.tmnow, t1.ShowLevel, t2.WRZ, t2.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          (SELECT     STCD, STNM, LGTD, LTTD, STLC, Z AS zdt8, tmnow, ShowLevel
                                               FROM          ST_RIVER_D) t1 LEFT JOIN
                                                  (SELECT     STCD, ISNULL(WRZ, 0) AS WRZ, ISNULL(GRZ, 0) AS GRZ
                                                    FROM          ST_RVFCCH_B) t2 ON t1.STCD = t2.STCD
                       UNION ALL
                       SELECT     t1.STCD, t1.STNM, t1.LGTD, t1.LTTD, t1.STLC, t1.zdt8, t1.tmnow, t1.ShowLevel, t2.WRZ, t2.GRZ, 
                                             CASE WHEN WRZ - zdt8 = 0 THEN '达汛限' WHEN zdt8 > WRZ THEN '超汛限:' + CAST((zdt8 - WRZ) AS varchar(10)) ELSE '正常' END AS Msg
                       FROM         (SELECT     STCD, STNM, LGTD, LTTD, STLC, RZ AS zdt8, tmnow, ShowLevel
                                              FROM          ST_RSVR_D) t1 LEFT JOIN
                                                 (SELECT     STCD, BGMD, EDMD, ISNULL(FSLTDZ, 0) AS WRZ, 0 AS GRZ
                                                   FROM          ST_RSVRFSR_B) t2 ON t1.STCD = t2.STCD AND t1.tmnow BETWEEN CONVERT(varchar, YEAR(GETDATE()), 4) + '-' + SUBSTRING(t2.BGMD, 1, 
                                             2) + '-' + SUBSTRING(t2.BGMD, 3, 2) AND CONVERT(varchar, YEAR(GETDATE()), 4) + '-' + SUBSTRING(t2.EDMD, 1, 2) + '-' + SUBSTRING(t2.EDMD, 3, 2)) 
                      AS t INNER JOIN
                          (SELECT     STCD, STTP, RVNM
                            FROM          dbo.ST_STBPRP_B
                            WHERE      (STTP = 'ZZ')) AS tt ON t .STCD = tt.STCD
WHERE     (t .Msg <> '正常') AND t .tmnow >= dateadd([day], - 3, GETDATE());



Oracle版本:

--水库预警
create view v_SK_WarningInfo
as
SELECT     
  t.zdt8 AS s, t.tmnow, t.STCD , t.STNM ,
   t.STLC, t.LGTD, t.LTTD, t.ShowLevel, t.WRZ, t.GRZ, tt.STTP, tt.RVNM,t.Msg
FROM 
(
      SELECT 
          t1.STCD, t1.STNM, t1.LGTD, t1.LTTD, t1.STLC, t1.zdt8, t1.tmnow, t1.ShowLevel, t2.WRZ, t2.GRZ, 
          CASE WHEN WRZ - zdt8 = 0 THEN '达警戒' WHEN zdt8 BETWEEN WRZ AND GRZ THEN '超警戒:' || CAST((NVL(zdt8,0) - NVL(WRZ,0)) AS varchar2(10)) 
          WHEN zdt8 > GRZ THEN '超保证:' || CAST((NVL(zdt8,0) - NVL(GRZ,0)) AS varchar2(10)) ELSE '正常' END AS Msg
      FROM          
      (
          SELECT     
              STCD, STNM, LGTD, LTTD, STLC, Z AS zdt8, tmnow, ShowLevel
          FROM         
          ST_RIVER_D 
      ) t1
      LEFT JOIN
      (
         SELECT     
             STCD, NVL(WRZ, 0) AS WRZ, NVL(GRZ, 0) AS GRZ
         FROM 
         ST_RVFCCH_B 
      ) t2 
      ON t1.STCD = t2.STCD
              
      UNION ALL
    
      SELECT 
          t1.STCD, t1.STNM, t1.LGTD, t1.LTTD, t1.STLC, t1.zdt8, t1.tmnow, t1.ShowLevel, t2.WRZ, t2.GRZ, 
          CASE WHEN WRZ - zdt8 = 0 THEN '达汛限' WHEN zdt8 > WRZ THEN '超汛限:' || CAST((zdt8 - WRZ) AS varchar2(10)) ELSE '正常' END AS Msg
       FROM
       ( 
           SELECT
               STCD, STNM, LGTD, LTTD, STLC, RZ AS zdt8, tmnow, ShowLevel
           FROM 
           ST_RSVR_D 
        ) t1
       LEFT JOIN
       (
           SELECT
               STCD, BGMD, EDMD, NVL(FSLTDZ, 0) AS WRZ, 0 AS GRZ
           FROM  
           ST_RSVRFSR_B
       ) t2 
       ON t1.STCD = t2.STCD AND 
          t1.tmnow BETWEEN (select to_char(sysdate,'yyyy') from dual) || '/' || substr(t2.BGMD, 1, 
          2) || '/' || substr(t2.BGMD, 3, 2) AND (select to_char(sysdate,'yyyy') from dual) || '/' || substr(t2.EDMD, 1, 2) || '/' || substr(t2.EDMD, 3, 2)
)  t 
INNER JOIN
(
   SELECT STCD, STTP, RVNM FROM ST_STBPRP_B
   WHERE STTP = 'RR'
)  tt
ON t .STCD = tt.STCD
WHERE 
t.Msg != '正常' AND t.tmnow >= (select sysdate-1 from dual);




--河道预警
create view v_HD_WarningInfo
as
SELECT 
    t.zdt8 AS s, t.tmnow, t.STCD, t.STNM, t.STLC, t.LGTD, 
    t.LTTD, t.ShowLevel, t.WRZ, t.GRZ, tt.STTP, tt.RVNM, t .Msg
    FROM 
    (
         SELECT  t1.STCD, t1.STNM, t1.LGTD, t1.LTTD, t1.STLC, t1.zdt8, t1.tmnow, t1.ShowLevel, t2.WRZ, t2.GRZ, 
            CASE WHEN WRZ - zdt8 = 0 THEN '达警戒' WHEN zdt8 BETWEEN WRZ AND GRZ THEN '超警戒:' || CAST((NVL(zdt8,0) - NVL(WRZ,0)) AS varchar2(10)) 
            WHEN zdt8 > GRZ THEN '超保证:' || CAST((NVL(zdt8,0) - NVL(GRZ,0)) AS varchar2(10)) ELSE '正常' END AS Msg
         FROM
         (    
             SELECT STCD, STNM, LGTD, LTTD, STLC, Z AS zdt8, tmnow, ShowLevel
                      FROM  ST_RIVER_D
         ) t1
         LEFT JOIN
         (
             SELECT STCD, NVL(WRZ, 0) AS WRZ, NVL(GRZ, 0) AS GRZ
                    FROM  ST_RVFCCH_B
         ) t2 ON t1.STCD = t2.STCD
         
        UNION ALL
        
        SELECT  t1.STCD, t1.STNM, t1.LGTD, t1.LTTD, t1.STLC, t1.zdt8, t1.tmnow, t1.ShowLevel, t2.WRZ, t2.GRZ, 
                CASE WHEN WRZ - zdt8 = 0 THEN '达汛限' WHEN zdt8 > WRZ THEN '超汛限:' || CAST((zdt8 - WRZ) AS varchar(10)) ELSE '正常' END AS Msg
           FROM 
           (
               SELECT  STCD, STNM, LGTD, LTTD, STLC, RZ AS zdt8, tmnow, ShowLevel
                    FROM  ST_RSVR_D
           ) t1 
           LEFT JOIN
           (
                  SELECT STCD, BGMD, EDMD, NVL(FSLTDZ, 0) AS WRZ, 0 AS GRZ
                        FROM  ST_RSVRFSR_B
           ) t2 ON t1.STCD = t2.STCD 
           AND t1.tmnow BETWEEN (select to_char(sysdate,'yyyy') from dual) || '/' || substr(t2.BGMD, 1, 
          2) || '/' || substr(t2.BGMD, 3, 2) AND (select to_char(sysdate,'yyyy') from dual) || '/' || substr(t2.EDMD, 1, 2) || '/' || substr(t2.EDMD, 3, 2)
)  t 
INNER JOIN
(
   SELECT 
    STCD, STTP, RVNM
    FROM   ST_STBPRP_B
    WHERE   
    STTP = 'ZZ'
)  tt 
ON t.STCD = tt.STCD
WHERE 
t.Msg != '正常' AND  t.tmnow >= (select sysdate-1 from dual);


相关文章
|
SQL XML Oracle
Mybatis动态SQL语句查询,实现一个参数 可查询多个字段。
Mybatis动态SQL语句查询,实现一个参数 可查询多个字段。
503 0
Mybatis动态SQL语句查询,实现一个参数 可查询多个字段。
|
5月前
|
SQL Oracle 关系型数据库
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
mysql和oracle 命令行执行sql文件 数据库执行sql文件 执行sql语句
71 0
|
SQL 存储 缓存
一文搞懂MySQL中一条SQL语句是如何执行的
一文搞懂MySQL中一条SQL语句是如何执行的
|
SQL 存储 关系型数据库
MySQL的第一篇文章——了解数据库、简单的SQL语句
MySQL的第一篇文章——了解数据库、简单的SQL语句
|
SQL Python
Pandas与SQL的数据操作语句对照
Pandas与SQL的数据操作语句对照
160 0
Pandas与SQL的数据操作语句对照
|
SQL 数据库 Python
Python 数据库Insert语句脚本生成工具(SQL Server)
Python 数据库Insert语句脚本生成工具(SQL Server)
381 0
Python 数据库Insert语句脚本生成工具(SQL Server)
|
SQL 程序员 开发工具
【Sql Server】基础之分组查询重复出现多条记录的SQL语句,以及group by和having、min函数运用
基础之分组查询重复出现多条记录的SQL语句,以及group by和having、min函数运用
531 0
【Sql Server】基础之分组查询重复出现多条记录的SQL语句,以及group by和having、min函数运用
SQL 查询表中每门课程成绩最好的前n名学生 优于group by语句的方法
SQL 查询表中每门课程成绩最好的前n名学生 优于group by语句的方法
SQL 查询表中每门课程成绩最好的前n名学生 优于group by语句的方法
|
SQL 数据库 数据安全/隐私保护
使用SQL语句 查询电话号码 加密显示
使用SQL语句 查询电话号码 加密显示
327 0
使用SQL语句 查询电话号码 加密显示
|
SQL 存储 安全
MYSQL数据库初窥门径, SQL语句地熟练使用, 图形化界面提高效率
MYSQL数据库初窥门径, SQL语句地熟练使用, 图形化界面提高效率
MYSQL数据库初窥门径, SQL语句地熟练使用, 图形化界面提高效率