1

我对 SQL 编程很陌生。我正在尝试修改每月运行时报告,以便我可以获得每周时间间隔的信息。当开始时间和结束时间在同一个月时,我的代码只输出 0。我不知道为什么。

Function TagCount ( Tag1Name char(24), Tag1Cond integer, Tag2Name char(24), Tag2Cond integer, StartTime timestamp, EndTime timestamp)
local count real;

count = (select count(*) from
(SELECT NAME,TS,VALUE AS V1VALUE FROM HISTORY WHERE (NAME = Tag2Name)
AND (PERIOD = '0:00:15')
AND (REQUEST='1')
AND (STEPPED='1')
AND TS between StartTime  and EndTime  JOIN
(SELECT NAME,TS,VALUE AS F1VALUE FROM HISTORY WHERE (NAME = Tag1Name)
AND (PERIOD = '0:00:15')
AND (REQUEST='1')
AND (STEPPED='1')
AND TS between StartTime  and EndTime ) USING (TS)) where V1VALUE = Tag2Cond and F1Value = Tag1Cond);

Return(count);
END
local
    starttime timestamp,
    run_hours real,
    i integer,
    endtime timestamp;

starttime = '01-JUN-10 00:00:00.0';
endtime = '12-AUG-10 00:00:00.0';

write ',';
FOR i=1 TO 21 DO

run_hours = 1/240.0*TagCount('runningtag',1,'producttag',i,starttime,endtime);
IF run_hours IS NULL THEN run_hours = 0; END
write run_hours;            
END

样本数据:

名称 TS F1VALUE 运行标签
16-AUG-10 15:35:30.1 1 个运行
标签 16-AUG-10 15:35:45.1 1 个运行
标签 16-AUG-10 15:36:00.1 1 个
运行标签 16-AUG-10 15:36:15.1 1 个
运行标签 16-AUG-10 15:36:30.1 1 个
运行标签 16-AUG-10 15:36:45.1 1 个
运行标签 16-AUG-10 15:37:00.1 1

名称 TS F1VALUE 产品代码
16-AUG-10 15:35:30.1 13
产品代码 16-AUG-10 15:35:45.1 13
产品代码 16-AUG-10 15:36:00.1 13
产品代码 16-AUG-10 15:36:15.1 13
产品代码 16-AUG-10 15:36:30.1 13
产品代码 16-AUG-10 15:36:45.1 13
产品代码 16-AUG-10 15:37:00.1 13

我试图以小时为单位估计运行时间。只是为了澄清。

4

1 回答 1

1

首先,您可以尝试将 SQL 简化为:

SELECT count(*)
  FROM history h
 WHERE h.period = '0:00:15'
       AND h.request = '1'
       AND h.stepped = '1'
       AND h.ts BETWEEN StartTime AND EndTime
       AND (   (h.name = Tag1Name AND h.value = Tag1Cond) 
            OR (h.name = Tag2Name AND h.value = Tag2Cond));

除此之外,我不明白为什么您不会在同一个月内获得两个日期的值 - 除非您的数据仅包含每月条目,我猜情况并非如此。

你能从历史表中发布一些示例数据吗?

于 2010-08-16T19:21:14.703 回答