LOOP
RETURN NEXT rec;
END LOOP;
END;
$$;
CREATE FUNCTION task6(DATE) RETURNS SETOF record LANGUAGE plpgsql SECURITY DEFINER AS
$$
DECLARE
rec RECORD;
BEGIN
FOR rec IN SELECT name FROM List WHERE(date_=$1)
LOOP
RETURN NEXT rec;
END LOOP;
END;
$$;
CREATE FUNCTION task7(INT, INT, INT) RETURNS SETOF record LANGUAGE plpgsql SECURITY DEFINER AS
$$
DECLARE
rec RECORD;
BEGIN
FOR rec IN SELECT name, weight, title FROM List, Manufacturer WHERE(manuf_id = $1 AND manuf_id = Manufacturer.id AND weight > $2 AND weight < $3)
LOOP
RETURN NEXT rec;
END LOOP;
END;
$$;
CREATE FUNCTION task8(DATE, DATE) RETURNS SETOF record LANGUAGE plpgsql SECURITY DEFINER AS
$$
DECLARE
rec RECORD;
BEGIN
FOR rec IN SELECT count(*)/table1.col::float*100::float AS percent FROM Sales, (SELECT count(*) AS col FROM Sales) AS table1 WHERE(date_ > $1 AND date_ < $2) GROUP BY table1.col
LOOP
RETURN NEXT rec;
END LOOP;
END;
$$;
9. Найти самый популярный вид электроприбора (продано наибольшее количество).
CREATE FUNCTION task9() RETURNS SETOF record LANGUAGE plpgsql SECURITY DEFINER AS
$$
DECLARE
rec RECORD;
BEGIN
FOR rec IN SELECT name, Sales.n_sales FROM List, Sales, Types WHERE(type_id=Types.id) ORDER BY n_sales desc LIMIT 1
LOOP
RETURN NEXT rec;
END LOOP;
END;
$$;
CREATE FUNCTION task10(INT, FLOAT) RETURNS SETOF record LANGUAGE plpgsql SECURITY DEFINER AS
$$
DECLARE
rec RECORD;
BEGIN
FOR rec IN SELECT count(*)/table1.col::float*100::float AS percent FROM List, (SELECT count(*) AS col FROM List) AS table1 WHERE(sup_id = $1 AND price < $2) GROUP BY table1.col
LOOP
RETURN NEXT rec;
END LOOP;
END;
$$;
CREATE FUNCTION task11(INT, INT) RETURNS SETOF record LANGUAGE plpgsql SECURITY DEFINER AS
$$
DECLARE
rec RECORD;
BEGIN
FOR rec IN SELECT name, Content.defect, Supplier.name_sup, Manufacturer.country FROM List, Content, Supplier, Manufacturer WHERE(Manufacturer.id=$1 AND Supplier.id=$2 AND Manufacturer.id=manuf_id AND Supplier.id=sup_id)
LOOP
RETURN NEXT rec;
END LOOP;
END;
$$;
CREATE FUNCTION task12(DATE, DATE) RETURNS SETOF record LANGUAGE plpgsql SECURITY DEFINER AS
$$
DECLARE
rec RECORD;
BEGIN
FOR rec IN SELECT avg(price) AS sr_price FROM List WHERE(date_ > $1 AND date_ < $2)
LOOP
RETURN NEXT rec;
END LOOP;
END;
$$;
CREATE FUNCTION task13(INT) RETURNS SETOF record LANGUAGE plpgsql SECURITY DEFINER AS
$$
DECLARE
rec RECORD;
sr_price float = avg(price) FROM List WHERE (manuf_id = $1);
BEGIN
FOR rec IN SELECT name, price FROM List WHERE(price > sr_price)
LOOP
RETURN NEXT rec;
END LOOP;
END;
$$;
Функции: task1
Входные параметры: вид электроприбора (varchar).
Вызов:
select * from task1(…) as (name varchar, name_sup varchar, title varchar, weight int, price float);
Возвращает: название, периодичность, издательство, тираж, цена.
Функции: task2a(),task2b (),task2c()
Входные параметры: вид электроприбора (varchar).
Вызов:
select * from task2a(…) as (name varchar, name_sup varchar, title varchar, weight int, price float);
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.