SQL select - Group by和concatenate(没有group_concat)

时间:2016-08-01 15:41:43

标签: sql select concatenation informix

我需要帮助解决IBM Informix 12.10上的问题。数据库。

Initial table: "toys"

product     | colour 
-----------------------
balloon     | red
balloon     | green
balloon     | white  
balloon     | yellow  
rubber duck | yellow 
rubber duck | white 

我尝试过STUFF,GROUP_CONCAT和LISTAGG。

product     | colours 
-----------------------
balloon     | red, green, white, yellow
rubber duck | yellow, white

结果表应如下所示:

{{1}}

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

在Oracle中,listagg()函数可以解决问题。希望Informix具有类似的功能:

SQL> column PRODUCT format A20
SQL> column COLOURS format A40
SQL> with tbl(product, colour) as (
     select 'balloon', 'red' from dual union
     select 'balloon', 'green' from dual union
     select 'balloon', 'white' from dual union
     select 'balloon', 'yellow' from dual union
     select 'rubber duck', 'yellow' from dual union
     select 'rubber duck', 'white' from dual
   )
   select product, listagg(colour, ', ') within group (order by colour) as colours
   from tbl
   group by product;

PRODUCT              COLOURS
-------------------- ----------------------------------------
balloon              green, red, white, yellow
rubber duck          white, yellow

SQL>