Could someone help explain what's wrong with syntax for the following codes:
CREATE OR REPLACE FUNCTION linearly_decrement_offset(location_in text)
RETURNS void AS
$BODY$BEGIN
IF tempoffset.ts_insert <= (now() at time zone 'utc') - '15 minutes':: interval AND tempoffset.ts_insert > (now() at time zone 'utc') - '30 minutes':: interval THEN
UPDATE tempoffset
SET offset_factor = offset_factor * 0.75
WHERE tempoffset.location = location_in;
ELSIF tempoffset.ts_insert =< (now() at time zone 'utc') - '30 minutes'::interval AND tempoffset.ts_insert > (now() at time zone 'utc') - '45 minutes'::interval THEN
UPDATE tempoffset
SET offset_factor = offset_factor* 0.5
WHERE tempoffset.location = location_in;
ELSIF tempoffset.ts_insert =< (now() at time zone 'utc') - '45 minutes'::interval AND tempoffset.ts_inset > (now() at time zone 'utc') - '1 hour'::interval THEN
UPDATE tempoffset
SET offset_factor = offset_factor * 0.25
WHERE tempoffset.location = location_in;
ELSIF tempoffset.ts_insert < (now() at time zone 'utc') - '1 hour'::interval THEN
DELETE FROM tempoffset;
END IF;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION linearly_decrement_offset(text)
OWNER TO postgres;
I got the following error when tried to execute it.
ERROR: syntax error at or near "IF"
LINE 3: IF tempoffset.ts_insert <= (now() at time zone 'utc') - '15...
^
********** Error **********
ERROR: syntax error at or near "IF"
SQL state: 42601
Character: 9
1 - width_bucket(...)*0.25.width_bucketdoc. Also not quite sure why you want to store this in a temp table instead of calculating on the fly.create functionstatement.selectquery to retrieve data from a table. Although this looks like as if the you don't need an IF at all. This could probably be done in a single update statement (apart from thedelete). What exactly are you trying to achieve with that?offset_factorvalue fromtempoffsettable. So, for example decrease theoffset_factorvalue by 25% if the existing values are 15 minutes and older but less than 30 mintues from the current query, and so on. I am trying to implement a REST API for an iOS app.