Я нашел два функциональных формата для него. как сказал Лоренц, мы можем добавить предложение where в первом или, как я сказал, добавить в конце запроса. Вот два решения. первый:
CREATE OR REPLACE FUNCTION staff_clock(p_id int)
RETURNS TABLE (
id int
, year int
, فروردین interval,
اردیبهشت interval,
خرداد interval,
تیر interval,
مرداد interval,
شهریور interval,
مهر interval,
آبان interval,
آذر interval,
دی interval,
بهمن interval,
اسفند interval) AS
$func$
BEGIN
RETURN QUERY
with hours as (
select "Staff_Id",
"Date",
case
when row_number() over w % 2 = 0 then
TO_CHAR("Time" - lag("Time") over w,'HH24:MI:SS')
end as hours
from "Org"."Clock"
window w as (partition by "Staff_Id", "Date" order by "Time")
), hours_per_month as (
select "Staff_Id",
extract(year from "Date")::int as work_year,
extract(month from "Date")::int as work_month,
sum(hours::interval) work_hours
from hours
where hours is not null
group by "Staff_Id", work_year, work_month
)
select "Staff_Id",
work_year,
sum("work_hours") filter (where work_month = 1) as فروردین,
sum("work_hours") filter (where work_month = 2) as اردیبهشت,
sum("work_hours") filter (where work_month = 3) as خرداد,
sum("work_hours") filter (where work_month = 4) as تیر,
sum("work_hours") filter (where work_month = 5) as مرداد,
sum("work_hours") filter (where work_month = 6) as شهریور,
sum("work_hours") filter (where work_month = 7) as مهر,
sum("work_hours") filter (where work_month = 8) as آبان,
sum("work_hours") filter (where work_month = 9) as آذر,
sum("work_hours") filter (where work_month = 10) as دی,
sum("work_hours") filter (where work_month = 11) as بهمن,
sum("work_hours") filter (where work_month = 12) as اسفند
from hours_per_month
group by "Staff_Id", work_year
having "Staff_Id"=p_id;
END
$func$ LANGUAGE plpgsql;
select * from staff_clock(2);
второй:
CREATE OR REPLACE FUNCTION staff_clock(p_id int)
RETURNS TABLE (
id int,
year int,
فروردین interval,
اردیبهشت interval,
خرداد interval,
تیر interval,
مرداد interval,
شهریور interval,
مهر interval,
آبان interval,
آذر interval,
دی interval,
بهمن interval,
اسفند interval)
AS $$
BEGIN
RETURN QUERY
with hours as (
select "Staff_Id",
"Date",
case
when row_number() over w % 2 = 0 then
TO_CHAR("Time" - lag("Time") over w,'HH24:MI:SS')
end as hours
from "Org"."Clock"
where "Staff_Id" = $1
window w as (partition by "Staff_Id", "Date" order by "Time")
), hours_per_month as (
select "Staff_Id",
extract(year from "Date")::int as work_year,
extract(month from "Date")::int as work_month,
sum(hours::interval) work_hours
from hours
where hours is not null
group by "Staff_Id", work_year, work_month
)
select "Staff_Id",
work_year,
sum("work_hours") filter (where work_month = 1) as فروردین,
sum("work_hours") filter (where work_month = 2) as اردیبهشت,
sum("work_hours") filter (where work_month = 3) as خرداد,
sum("work_hours") filter (where work_month = 4) as تیر,
sum("work_hours") filter (where work_month = 5) as مرداد,
sum("work_hours") filter (where work_month = 6) as شهریور,
sum("work_hours") filter (where work_month = 7) as مهر,
sum("work_hours") filter (where work_month = 8) as آبان,
sum("work_hours") filter (where work_month = 9) as آذر,
sum("work_hours") filter (where work_month = 10) as دی,
sum("work_hours") filter (where work_month = 11) as بهمن,
sum("work_hours") filter (where work_month = 12) as اسفند
from hours_per_month
group by "Staff_Id", work_year;
END; $$
LANGUAGE 'plpgsql';
select * from staff_clock(3)