-- Funktion für PostgreSQL Server, für Clientskripte nicht relevant
-- FUNCTION: public.set_intervall(interval, timestamp with time zone, timestamp with time zone)

-- DROP FUNCTION public.set_intervall(interval, timestamp with time zone, timestamp with time zone);

CREATE OR REPLACE FUNCTION public.set_intervall(
	min_intervall interval,
	von_timestamp timestamp with time zone,
	bis_timestamp timestamp with time zone)
    RETURNS interval
    LANGUAGE 'sql'

    COST 100
    VOLATILE 
AS $BODY$
SELECT CASE
    WHEN (min_intervall <= '1 seconds'::interval     and '1 seconds'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('1 seconds'::interval)
    WHEN (min_intervall <= '10 seconds'::interval    and '10 seconds'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('10 seconds'::interval)
    WHEN (min_intervall <= '30 seconds'::interval    and '30 seconds'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('30 seconds'::interval)
    WHEN (min_intervall <= '1 minutes'::interval     and '1 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('1 minutes'::interval)
    WHEN (min_intervall <= '5 minutes'::interval     and '5 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('5 minutes'::interval)
    WHEN (min_intervall <= '15 minutes'::interval    and '15 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('15 minutes'::interval)
    WHEN (min_intervall <= '30 minutes'::interval    and '30 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('30 minutes'::interval)
    WHEN (min_intervall <= '60 minutes'::interval    and '60 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('60 minutes'::interval)
    WHEN (min_intervall <= '120 minutes'::interval   and '120 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('120 minutes'::interval)
    WHEN (min_intervall <= '240 minutes'::interval   and '240 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('240 minutes'::interval)
    WHEN (min_intervall <= '480 minutes'::interval   and '480 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('480 minutes'::interval)
    WHEN (min_intervall <= '720 minutes'::interval   and '720 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('720 minutes'::interval)
    WHEN (min_intervall <= '1440 minutes'::interval  and '1440 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('1440 minutes'::interval)
    WHEN (min_intervall <= '2880 minutes'::interval  and '2880 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('2880 minutes'::interval)
    WHEN (min_intervall <= '10080 minutes'::interval and '10080 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('10080 minutes'::interval)
    WHEN (min_intervall <= '43200 minutes'::interval and '43200 minutes'::interval > (bis_timestamp::timestamp - von_timestamp::timestamp) / 2000) THEN ('43200 minutes'::interval)
	ELSE ('43200 minutes'::interval)
END;

$BODY$;

ALTER FUNCTION public.set_intervall(interval, timestamp with time zone, timestamp with time zone)
    OWNER TO hofei;

