sql - Group By Result Into a List -
i trying list of rental properties manager manages schema:
create table rental_property(property_number int primary key, managerid int, foreign key(managerid) references manage(managerid));
here procedure:
create or replace procedure supervisor_properties list_of_properties varchar (300) := ' '; begin select 'manager' || ': ' || managerid || ' ' || property_number list_of_properties rental_property group managerid; end;
the part having trouble procedure above group by
group tuples have same managerid together. how print out results this:
manager m1: rental_prop1, rental_prop2, rental_prop3 manager m2: rental_prop9, rental_prop6, rental_prop4
you can use list_agg()
:
select (managerid || ' ' || list_agg(property_number, ' ') within group (order property_number) ) list_of_properties rental_property group managerid;
the issue into
putting value variable . . . , generate error if group by
has multiple managers.
start query:
select managerid, list_agg(property_number, ' ') within group (order property_number) properties rental_property group managerid;
edit:
i see. if want print values:
create or replace procedure supervisor_properties begin x in (select managerid, list_agg(property_number, ' ') within group (order property_number) properties rental_property group managerid ) loop dbms_output.put_line(x.managerid || ' ' || x.properties); end loop; end;
Comments
Post a Comment