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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -