CREATE TYPE prueba1 AS
(almacen_name VARCHAR(20),
total_transactions INTEGER);
CREATE FUNCTION shipped_received_diff()
RETURNS SETOF prueba1 AS $$
DECLARE
good_received prueba1.total_transactions%TYPE;
good_shipped prueba1.total_transactions%TYPE;
total prueba1.total_transactions%TYPE;
almacen prueba1.almacen_name%TYPE;
almacen_transactions prueba1;
BEGIN
FOR almacen_transactions in SELECT wh_name,0 AS total
FROM tb_transaction JOIN tb_warehouse ON trans_wh_code=wh_code LOOP
good_received:=transactions_quantity (almacen,'TT101');
good_shipped:=transactions_quantity (almacen,'TT601');
total:=good_received-good_shipped;
RETURN NEXT almacen_transactions;
END LOOP;
RETURN;
END;
$$LANGUAGE plpgsql;