我对 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
我试图以小时为单位估计运行时间。只是为了澄清。