create or replace function get_top_clients_by_calories(p_id int, start_date date, end_date date) returns table(member_id int, first_name varchar, last_name varchar, total_calories decimal) language plpgsql as $$ begin return query select m.id as member_id, m.first_name, m.last_name, sum((v.duration_minutes / 60.0) * w.calories_per_hour) as total_calories from visits v join members m on v.member_id = m.id join workouts w on v.workout_id = w.id where v.visit_date between start_date and end_date group by m.id, m.first_name, m.last_name order by total_calories desc limit p_id; end $$;
create table employees (id serial primary key, first_name varchar(50), last_name varchar(50), salary decimal(10, 2), vacation_start date, vacation_end date);
insert into employees values (1, 'Иван', 'Иванов', 70000.00, '2026-06-01', '2026-06-15');
create or replace function get_top_clients_by_calories(p_id int, start_date date, end_date date) returns table(member_id int, first_name varchar, last_name varchar, total_calories decimal) language plpgsql as $$ begin return query select m.id as member_id, m.first_name, m.last_name, sum((v.duration_minutes / 60.0) * w.calories_per_hour) as total_calories from visits v join members m on v.member_id = m.id join workouts w on v.workout_id = w.id where v.visit_date between start_date and end_date group by m.id, m.first_name, m.last_name order by total_calories desc limit p_id; end $$;
create or replace view v_employee_vacations as select id, first_name, last_name, salary as monthly_salary, vacation_start, vacation_end, calculate_vacation_pay(salary, vacation_start, vacation_end) as vacation_pay from employees;