I'm writing function in plpgsql and stuck with following question: my function has parameter service_id_input
and query. I want to create additional clause in WHERE section if this variable is not null. For example
SELECT
MIN(time_from) AS min_0,
MAX(time_to) AS max_0,
CEIL((MAX(time_to) * 60 - MIN(time_from) * 60) / reception_duration_input) AS diff_0,
user_id AS user_id_0,
workingtimetable.date AS date_0
FROM
workingtimetable
WHERE
-- IF service_id_input is not null then add another restriction
department_id = department_id_input AND date >= NOW() :: DATE
GROUP BY user_id, date
ORDER BY date ASC;
How can I do it?
Full code of function:
CREATE OR REPLACE FUNCTION find_days_business(IN reception_duration_input INTEGER, IN department_id_input BIGINT, IN service_id_input BIGINT)
RETURNS TABLE(avail_records NUMERIC, busy_records BIGINT, percentage NUMERIC, day_date DATE) AS
$BODY$
DECLARE
BEGIN
CREATE TEMP TABLE sum_of_avail_records ON COMMIT DROP AS
SELECT
SUM(diff_0) AS sum_0,
date_0
FROM (
SELECT
MIN(time_from) AS min_0,
MAX(time_to) AS max_0,
CEIL((MAX(time_to) * 60 - MIN(time_from) * 60) / reception_duration_input) AS diff_0,
user_id AS user_id_0,
workingtimetable.date AS date_0
FROM workingtimetable
WHERE
-- IF service_id_input is not null then add another restriction
department_id = department_id_input AND date >= NOW() :: DATE
GROUP BY user_id, date
ORDER BY date ASC
) AS perday
GROUP BY date_0;
CREATE TEMP TABLE num_of_busy_records ON COMMIT DROP AS
SELECT
COUNT(appointments_book_records.id) AS count_1,
appointments_book_days.date AS date_1
FROM appointments_book_records
INNER JOIN appointments_book_days ON appointments_book_records.appointment_day_id = appointments_book_days.id
WHERE appointments_book_days.date >= NOW() :: DATE
GROUP BY appointments_book_days.date
ORDER BY appointments_book_days.date;
RETURN QUERY
SELECT
sum_0,
count_1,
(count_1 / sum_0 * 100),
date_0
FROM sum_of_avail_records
LEFT OUTER JOIN num_of_busy_records ON date_0 = date_1;
END;$BODY$
LANGUAGE plpgsql;
AND (service_id_input IS NULL OR (<another restriction>))