Search code examples
sqloracle-databasejoinstring-aggregation

Oracle Sql select all values in a row without using aggregate functions and procedures


I have this code:

select eto.id,( epe.surname || ' ' || epe.name  || ' ' || epe.patronymic) fio
                        from employee_trip_orders eto
                        left join employee_card_order_links ecol
                        on eto.id = ecol.trip_order_id
                        left join employee_trip_cards etc
                        on ecol.trip_card_id = etc.employee_trip_card_id 
                        left join eip_persons.employees epe
                        on etc.employee_id = epe.employee_id
                        where eto.id=2223
                        order by eto.id

and result of executing this code

id     fio
------------
2223 John Smith Brown
2223 Jack Orange Apple
2223 Jordan Stinky Tomato

I would like to transform this into single row with comma separators

id     fio
-------------------------------
2223 |  John Smith Brown,
     |  Jack Orange Apple,
     |  Jordan Stinky Tomato
-------------------------------

How can i achieve this without using external functions, group by and agregate functions? Thanks


Solution

  • You can do it using connect by query, but it is slower and more complicated than simple listagg().

    select id, ltrim(sys_connect_by_path(fio, ', '), ', ') names
      from (select row_number() over (partition by id order by fio) rn, id, fio from t)
      where connect_by_isleaf = 1
      connect by id = prior id and rn = prior rn + 1
      start with rn = 1
    

    dbfiddle