获取两个日期之间的总机器运行时间,但分为 3 个时间范围:标准时间、高峰时间和非高峰时间。
上下文
编程环境: Wonderware ArchestrA
编程语言: ArchestrA Quick Script .Net
数据库:Historian - SQL Server (In-SQL)
外部:采矿业的几个水泵,需要知道水泵在 3 个不同的电价时间(峰值,标准,非高峰时间)。
工作日:
标准时间: 09:00 至 17:00 和 19:00 至 22:00
高峰时间: 06:00 至 09:00 和 17:00 至 19:00
非高峰时间: 22: 00 至 06:00
周六:
标准时间: 07:00 至 12:00 和 18:00 至 20:00
非高峰时间: 20: 00 至 07:00 和 12:00 至 18:00
周日:
非高峰时间:整个周日都是非高峰 时间
我需要
在两个日期之间:
- 泵在高峰时间运行的总小时数。
- 泵在非高峰时间运行的总小时数。
- 泵在标准时间运行的总小时数。
我试过的:(机器运行的总非高峰时间(以小时为单位)。
它有效,但大多数时候我得到的时间少于我应该得到的时间。
-- This script only gets the total off-peak time hours
SET NOCOUNT ON
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
DECLARE @var1 REAL;
DECLARE @var2 REAL;
DECLARE @var3 REAL;
SET @StartDate = '2015/08/01 05:00:00.000'
SET @EndDate = GetDate()
SET NOCOUNT OFF
SET @var1 =
(
SELECT
'Count' = Count(DiscreteHistory.Value)/60.0
FROM
DiscreteHistory
WHERE
DiscreteHistory.TagName
IN ('KDCE_S04_22PMP01_Machine.FA_RF')
AND DiscreteHistory.Value = 1
AND wwRetrievalMode = 'Cyclic'
AND wwResolution = 60000
AND DateTime >= @StartDate
AND DateTime <= @EndDate
AND DATEPART(dw, DateTime) NOT IN (2, 3, 4, 5, 6, 7)
)
SET @var2 =
(
SELECT
'Count' = Count(DiscreteHistory.Value)/60.0
FROM
DiscreteHistory
WHERE
DiscreteHistory.TagName
IN ('KDCE_S04_22PMP01_Machine.FA_RF')
AND DiscreteHistory.Value = 1
AND wwRetrievalMode = 'Cyclic'
AND wwResolution = 60000
AND DateTime >= @StartDate
AND DateTime <= @EndDate
AND DATEPART(dw, DateTime) NOT IN (1, 2, 3, 4, 5, 6)
AND (CAST(DateTime as time) >= '20:00:00' AND CAST(DateTime as time) < '07:00:00')
)
SET @var3 =
(
SELECT
'Count' = Count(DiscreteHistory.Value)/60.0
FROM
DiscreteHistory
WHERE
DiscreteHistory.TagName
IN ('KDCE_S04_22PMP01_Machine.FA_RF')
AND DiscreteHistory.Value = 1
AND wwRetrievalMode = 'Cyclic'
AND wwResolution = 60000
AND DateTime >= @StartDate
AND DateTime <= @EndDate
AND DATEPART(dw, DateTime) NOT IN (1, 2, 3, 4, 5, 6)
AND (CAST(DateTime as time) >= '12:00:00' AND CAST(DateTime as time) < '18:00:00')
)
IF @var1 IS NULL SET @var1 = 0
IF @var2 IS NULL SET @var2 = 0
IF @var3 IS NULL SET @var3 = 0
SELECT
'Count' = (Count(DiscreteHistory.Value)/60.0) + @var1 + @var2 + @var3
FROM
DiscreteHistory
WHERE
DiscreteHistory.TagName
IN ('KDCE_S04_22PMP01_Machine.FA_RF')
AND DiscreteHistory.Value = 1
AND wwRetrievalMode = 'Cyclic'
AND wwResolution = 60000
AND DateTime >= @StartDate
AND DateTime <= @EndDate
AND DATEPART(dw, DateTime) NOT IN (1, 7)
AND (CAST(DateTime as time) >= '22:00:00' OR CAST(DateTime as time) < '06:00:00');
谢谢你。
样本数据
我将以下信息记录到数据库中:
运行反馈的唯一标签名称:KDCE_S04_22PMP01_Machine.FA_RF这是一个运行反馈,它是“1”或“0”或“空”值
机器运行小时数的唯一标记名称:me.a0_MainPump.RunningHours.FA_PV,它是泵运行小时数的整数值。
两个标签名称都记录有 TagName、Value、DateTime、质量等。
我有一个包含以下列的表:
| DateTime | TagName | Value | QualityDetail |
在 DB 中获取样本数据的脚本:
SET NOCOUNT ON
DECLARE @StartDate DateTime
DECLARE @EndDate DateTime
SET @StartDate = '20150701 05:00:00.000'
SET @EndDate = '20150731 05:00:00.000'
SET NOCOUNT OFF
SELECT
DateTime, TagName, Value, Quality
FROM
DiscreteHistory
WHERE
DiscreteHistory.TagName IN ('KDCE_S04_22PMP01_Machine.FA_RF')
AND DateTime >= @StartDate AND DateTime <= @EndDate
如果我导出到 csv,它会返回这个输出:(我已经缩短了它)
DateTime,TagName,Value,Quality
2015/07/01 05:00:00 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,133
2015/07/01 05:09:46 AM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/01 05:09:53 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/01 06:44:20 AM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/01 06:45:54 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/01 07:36:22 AM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/01 07:36:48 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/01 01:53:44 PM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/01 01:53:44 PM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/01 02:04:52 PM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/01 02:05:27 PM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/01 02:07:25 PM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/01 02:09:13 PM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/01 02:14:54 PM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/02 12:10:48 AM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/02 05:24:06 AM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/02 05:24:16 AM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/02 05:50:52 AM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/02 05:50:59 AM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/02 06:00:15 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/02 06:55:18 AM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/02 06:55:18 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/02 09:46:58 AM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/02 09:46:58 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/02 01:30:27 PM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/02 01:30:27 PM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/02 05:38:03 PM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/02 07:01:56 PM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/03 03:41:09 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/03 09:05:18 AM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/03 10:42:00 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/03 10:57:31 AM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/03 04:53:36 PM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/04 10:08:17 PM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/05 06:43:50 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/05 09:43:08 AM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/05 01:04:03 PM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/06 09:37:53 AM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/06 11:07:15 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/06 11:29:48 AM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/06 05:02:38 PM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/07 06:15:33 AM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/07 06:32:24 AM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/07 09:05:20 AM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/07 01:10:09 PM,KDCE_S04_22PMP01_Machine.FA_RF,(null),1
2015/07/07 01:10:16 PM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/07 04:45:12 PM,KDCE_S04_22PMP01_Machine.FA_RF,0,0
2015/07/07 08:19:40 PM,KDCE_S04_22PMP01_Machine.FA_RF,1,0
2015/07/07 09:01:35 PM,KDCE_S04_22PMP01_Machine.FA_RF,0,0