DROP FUNCTION IF EXISTS public.get_contract_by_id(integer);

CREATE OR REPLACE FUNCTION public.get_contract_by_id(p_contract_id INTEGER)
RETURNS TABLE (
    Id                 INTEGER,
    ContractorIdNumber VARCHAR(20),
    ContractorName     VARCHAR(50),
    ContractorSurname  VARCHAR(100),
    ContractInitDate   TIMESTAMP,
    RateId             INTEGER,
    RateName           VARCHAR(100),
    RatePrice          MONEY
) AS $$
    SELECT c.Id,
           c.ContractorIdNumber,
           c.ContractorName,
           c.ContractorSurname,
           c.ContractInitDate,
           c.RateId,
           r.Name  AS RateName,
           r.Price AS RatePrice
    FROM   public.Contracts c
    JOIN   public.Rates r ON r.Id = c.RateId
    WHERE  c.Id = p_contract_id;
$$ LANGUAGE SQL STABLE;