hiten4github / tamt

Automatically exported from code.google.com/p/tamt
0 stars 0 forks source link

Dvision by zero blocks traffic count report generation #46

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
If traffic counts in vehicle flow component are in intervals of exact hours 
(e.g. 09:00 to 10:00), then a division by zero error occurs. If traffic counts 
are in intervals of partial hours (e.g. 10:00 to 10:45) then no error occurs

The problem is in this stored procedure:

-- Function: tamt_trafficflowhourlyaverage(text, text)

-- DROP FUNCTION tamt_trafficflowhourlyaverage(text, text);

CREATE OR REPLACE FUNCTION tamt_trafficflowhourlyaverage(_tagid text, _daytype 
text)
  RETURNS SETOF record AS
$BODY$
DECLARE
    my_record RECORD;
BEGIN

    FOR my_record IN SELECT
        id,
        _tagid,
        _daytype,
        date_part('hour', (starttime)) as hour,
        w2 * (60.0 / date_part('minute', (endtime - starttime))) as w2avg,
        w3 * (60.0 / date_part('minute', (endtime - starttime))) as w3avg,
        pc * (60.0 / date_part('minute', (endtime - starttime))) as pcavg,
        tx * (60.0 / date_part('minute', (endtime - starttime))) as txavg,
        ldv * (60.0 / date_part('minute', (endtime - starttime))) as ldvavg,
        ldc * (60.0 / date_part('minute', (endtime - starttime))) as ldcavg,
        hdc * (60.0 / date_part('minute', (endtime - starttime))) as hdcavg,
        mdb * (60.0 / date_part('minute', (endtime - starttime))) as mdbavg,
        hdb * (60.0 / date_part('minute', (endtime - starttime))) as hdbavg
    FROM
        trafficcount
    WHERE
        tagid = _tagid
    AND
        daytype = _daytype
    LOOP
        RETURN NEXT my_record;
    END LOOP;
END;
$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100
  ROWS 1000;
ALTER FUNCTION tamt_trafficflowhourlyaverage(text, text) OWNER TO postgres;

The date_part('minute', (endtime - starttime)) can produce a zero.

Original issue reported on code.google.com by stuartmo...@gmail.com on 18 Oct 2010 at 10:46

GoogleCodeExporter commented 9 years ago
Duplicate of Issue 55

Original comment by stuartmo...@gmail.com on 18 Jun 2011 at 12:29