From 8e75888a2fe93933cee9c0f94a9b2a817346c1be Mon Sep 17 00:00:00 2001 From: zch Date: Fri, 22 Nov 2024 10:54:14 +0800 Subject: [PATCH] =?UTF-8?q?ADDD(=E4=BD=9C=E4=B8=9A):=20=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E8=92=B8=E6=B1=BD=E6=95=B0=E6=8D=AE=E8=AE=B0=E5=BD=95=E5=AD=98?= =?UTF-8?q?=E5=82=A8=E8=BF=87=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建名为 [dbo].[Record_SteamPointData] 的存储过程 - 该存储过程用于记录蒸汽消耗数据 - 主要功能包括: - 获取上一个小时的时间范围 - 计算蒸汽消耗量 - 插入数据到 ems_report_point_steam 表中 - 使用了多个子查询和连接操作来获取所需数据 --- zhengqi.sql | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 zhengqi.sql diff --git a/zhengqi.sql b/zhengqi.sql new file mode 100644 index 0000000..97b268e --- /dev/null +++ b/zhengqi.sql @@ -0,0 +1,45 @@ + +CREATE PROCEDURE [dbo].[Record_SteamPointData] +AS +declare @begin_time datetime +declare @end_time datetime +BEGIN + set @begin_time = DATEADD(hh, -1, GETDATE()); + set @end_time = DATEADD(hh, -0, GETDATE()); + declare @dateinfo datetime; + set @dateinfo = GETDATE(); +INSERT INTO ems_report_point_steam (monitor_code, expend,instrument_value,begin_time,end_time,record_time) + SELECT + + t1.monitor_code AS monitor_code, + CONVERT(DECIMAL(18, 2), ISNULL((t3.steam_flow - t4.instrument_value),0)) AS expend, + ISNULL(t3.steam_flow,0) AS instrument_value, + DATEADD(HH, -1, @dateinfo) AS begin_time, + DATEADD(HH, 0, @dateinfo) AS end_time, + CONVERT(VARCHAR(10), DATEADD(HH, -1, @dateinfo), 120) AS record_time + FROM ems_base_monitor_info t1 + LEFT JOIN ( + SELECT T1.monitor_code, T1.steam_flow + FROM ems_record_steam_instant T1 + left JOIN ( + SELECT monitor_code, MAX(collect_time) AS collect_time + FROM ems_record_steam_instant + WHERE collect_time BETWEEN DATEADD(HH, -1, @dateinfo) AND DATEADD(HH, 0, @dateinfo) + GROUP BY monitor_code) T2 + on T1.collect_time = T2.collect_time AND T1.monitor_code = T2.monitor_code + WHERE T1.collect_time BETWEEN DATEADD(HH, -1, @dateinfo) AND DATEADD(HH, 0, @dateinfo) + ) t3 ON t1.monitor_code = t3.monitor_code + + + LEFT JOIN ( + SELECT E1.monitor_code,E1.begin_time,E1.instrument_value + FROM ems_report_point_steam E1 + JOIN ( + SELECT monitor_code, MAX(begin_time) AS begin_time + FROM ems_report_point_steam + GROUP BY monitor_code) E2 + on E1.begin_time = E2.begin_time AND E1.monitor_code = E2.monitor_code + ) t4 ON t1.monitor_code = t4.monitor_code; +END; +go +