这是一个Oracle的列转行函数:LISTAGG()
示例代码
- withtempas(@H_502_24@
- select'China'nation,'Guangzhou'cityfromdualunionall@H_502_24@
- 'Shanghai'city'Beijing'cityselect'USA'nation,'NewYork'city'Bostom'cityselect'Japan'nation,'Tokyo'cityfromdual@H_502_24@
- )@H_502_24@
- selectnation,listagg(city,',')withinGROUP(orderbycity)@H_502_24@
- fromtemp@H_502_24@
- groupbynation @H_502_24@
这是最基础的用法:
LISTAGG(XXX,XXX) WITHIN GROUP( ORDER BY XXX)
用法就像聚合函数一样,通过Group by语句,把每个Group的一个字段,拼接起来。
非常方便。
就是over(partition by XXX)
也就是说,在你不实用Group by语句时候,也可以使用LISTAGG函数:
- select500population,'China'nation,85); font-weight:bold">select1500population,85); font-weight:bold">select1000population,'USA'nation,'Japan'nation,85); font-weight:bold">selectpopulation,@H_502_24@
- nation,250)"> city,250)"> listagg(city,85); font-weight:bold">bycity)over(partitionbynation)rank@H_502_24@
- temp
@H_502_24@
总结:LISTAGG()把它当作SUM()函数来使用就可以了。
转载:http://dacoolbaby.iteye.com/blog/1698957