How to write a query that does something similar to MySQL's GROUP_CONCAT in Oracle?

前端 未结 3 1449
长发绾君心
长发绾君心 2020-12-12 03:03

When I run the following SQL query in my Oracle database:

SELECT p.pkt_nazwa, 
       u.us_nazwa 
FROM   punkty p, 
       kategorie_uslug ku, 
       usluga         


        
相关标签:
3条回答
  • 2020-12-12 03:29

    Here's an article explaining some approaches to what you're trying to do in Oracle:

    http://www.oracle-base.com/articles/misc/StringAggregationTechniques.php

    Note that if you're running 11g Release 2 then LISTAGG is the way to go.

    0 讨论(0)
  • 2020-12-12 03:39
    SELECT p.pkt_nazwa AS name, wm_concat(u.us_nazwa) AS services
    FROM   punkty p
    JOIN   kategorie_uslug ku ON ku.pkt_id = p.pktk_1_id
    JOIN   usluga u ON u.usk_1_id = ku.us_id
    GROUP  BY p.pkt_nazwa
    ORDER  BY p.pkt_nazwa;
    

    wm_concat() function is present in most versions.
    More about string aggregating in Oracle.

    0 讨论(0)
  • 2020-12-12 03:53

    Since Oracle 11.2 this has become easier using listagg()

    SELECT p.pkt_nazwa AS name, 
           listagg(u.us_nazwa) WITHIN GROUP (ORDER BY u.us_nazwa) AS services
    FROM punkty p
      JOIN kategorie_uslug ku ON ku.pkt_id = p.pktk_1_id
      JOIN usluga u ON u.usk_1_id = ku.us_id
    GROUP BY p.pkt_nazwa
    ORDER BY p.pkt_nazwa;
    
    0 讨论(0)
提交回复
热议问题