-- crea le funzioni MIN e MAX per oid, text, char, varchar, timestamp e time.

drop function timestampsmaller (timestamp,timestamp);
create function timestampsmaller (timestamp,timestamp) returns timestamp as
'
begin
     if $1 > $2 then
	return $2;
     else
	return $1;
     end if;
 end;
' language 'plpgsql';

drop aggregate min timestamp;
create aggregate min (basetype = timestamp, sfunc1 = timestampsmaller,
                      stype1 = timestamp, stype2 = timestamp);

drop function timestamplarger (timestamp,timestamp);
create function timestamplarger (timestamp,timestamp) returns timestamp as
'
begin
     if $1 < $2 then
	return $2;
     else
	return $1;
     end if;
 end;
' language 'plpgsql';

drop aggregate max timestamp;
create aggregate max (basetype = timestamp, sfunc1 = timestamplarger,
                      stype1 = timestamp, stype2 = timestamp);


drop function timesmaller (time,time);
create function timesmaller (time,time) returns time as
'
begin
     if $1 > $2 then
	return $2;
     else
	return $1;
     end if;
 end;
' language 'plpgsql';

drop aggregate min time;
create aggregate min (basetype = time, sfunc1 = timesmaller,
                      stype1 = time, stype2 = time);

drop function timelarger (time,time);
create function timelarger (time,time) returns time as
'
begin
     if $1 < $2 then
	return $2;
     else
	return $1;
     end if;
 end;
' language 'plpgsql';

drop aggregate max time;
create aggregate max (basetype = time, sfunc1 = timelarger,
                      stype1 = time, stype2 = time);

drop function oidsmaller (oid,oid);
create function oidsmaller (oid, oid) returns oid as
'
begin
     if $1 > $2 then
	return $2;
     else
	return $1;
     end if;
 end;
' language 'plpgsql';


drop function oidlarger (oid,oid);
create function oidlarger (oid, oid) returns oid as
'
begin
     if $1 < $2 then
	return $2;
     else
	return $1;
     end if;
 end;
' language 'plpgsql';

drop aggregate max oid;
create aggregate max (basetype = oid, sfunc1 = oidlarger,
                      stype1 = oid, stype2 = oid);
drop aggregate min oid;
create aggregate min (basetype = oid, sfunc1 = oidsmaller,
                      stype1 = oid, stype2 = oid);

drop function txtsmaller(text,text);
create function txtsmaller(text, text) returns text as
'
begin
     if $1 > $2 then
	return $2;
     else
	return $1;
     end if;
 end;
' language 'plpgsql';

drop aggregate min text;
create aggregate min (basetype = text, sfunc1 = txtsmaller,
                      stype1 = text, stype2 = text);

drop function txtlarger (text,text);
create function txtlarger (text, text) returns text as
'
begin
     if $1 < $2 then
	return $2;
     else
	return $1;
     end if;
 end;
' language 'plpgsql';

drop aggregate max text;
create aggregate max (basetype = text, sfunc1 = txtlarger,
                      stype1 = text, stype2 = text);
drop table b;
create table b (a varchar(6),d date, t time,dt timestamp, i interval);
insert into b values ('ZETA','12-05-1998','10:20','1997-11-21 10:23',interval '3:31');
insert into b values ('BETA','12-11-1998','22:50','1996-13-30 12:31',interval '2:10');
insert into b values ('ALFA','22-02-1996','12:36','1994-02-11 11:24',interval '1:30');
insert into b values ('GAMA','11-10-1968','16:30','1994-10-31 02:11',interval '2:33');
insert into b values ('NETA',current_date,current_time,current_timestamp,interval '2:33');
select min(a),min(oid),min(d),min(t),min(dt),min(i) from b;
select max(a),max(oid),max(d),max(t),max(dt),max(i) from b;
select oid,* from b;
