请检查下面的 PostgreSQL 函数 activeAppointments(),它接受输入日期和时间并返回在给定日期和时间活跃的约会数量。
CREATE OR REPLACE FUNCTION public.activeappointments(
ondate date,
fromtime time without time zone,
totime time without time zone)
RETURNS integer
LANGUAGE plpgsql
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
declare
total integer;
BEGIN
SELECT count(*) into total FROM appointments
where apt_date = ondate and apt_start >= fromTime and apt_end <= toTime;
RETURN total;
END;
$BODY$;
我采用了上面提到的“约会”表,并在下面插入了示例数据。
insert into appointments values ('Patient001', 'Doc001', '01-JAN-2021','120000','130000');
insert into appointments values ('Patient002', 'Doc001', '01-JAN-2021','133000','143000');
insert into appointments values ('Patient003', 'Doc001', '01-JAN-2021','150000','160000');
select count(1) from appointments;
SELECT activeAppointments('01-JAN-2021', '120000', '150000'); --2