试图将月度报告修改为每周一次。 Aspen Sql

时间:2010-08-16 17:29:19

标签: sql aspen

我是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

示例数据:

NAME TS F1VALUE
runningtag 16-AUG-10 15:35:30.1 1
runningtag 16-AUG-10 15:35:45.1 1
runningtag 16-AUG-10 15:36:00.1 1
runningtag 16-AUG-10 15:36:15.1 1
runningtag 16-AUG-10 15:36:30.1 1
runningtag 16-AUG-10 15:36:45.1 1
runningtag 16-AUG-10 15:37:00.1 1

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

我试图用小时来估算运行时间。只是为了澄清。

1 个答案:

答案 0 :(得分: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));

除此之外,我不明白为什么你不会在同一个月内获得两个日期的值 - 除非你的数据只包含月度条目,我估计不是这样。

您可以从历史记录表中发布一些样本数据吗?