Question

I am very new to SQL programming. I am trying to modify a monthly runtime report so that I can get the information on a weekly time interval. My code just outputs 0's when the starttime and endtime are in the same month. I can't figure out why.

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

Sample Data:

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
productcode 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
productcode 16-AUG-10 15:37:00.1 13

I am trying to estimate runtime in hours. Just to clarify.

Was it helpful?

Solution

Firstly, can you try simplifying your SQL to:

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));

Other than that, I can't see why you wouldn't get values for two dates in the same month -- unless your data only contained monthly entries, which I'm guessing isn't the case.

Could you post some sample data from the history table?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top