这是一个Oracle的列转行函数:LISTAGG()
示例代码
- withtempas(
- select'China'nation,'Guangzhou'cityfromdualunionall
- 'Shanghai'city'Beijing'cityselect'USA'nation,'NewYork'city'Bostom'cityselect'Japan'nation,'Tokyo'cityfromdual
- )
- selectnation,listagg(city,',')withinGROUP(orderbycity)
- fromtemp
- groupbynation
这是最基础的用法:
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,
- nation,250)"> city,250)"> listagg(city,85); font-weight:bold">bycity)over(partitionbynation)rank
- temp
总结:LISTAGG()把它当作SUM()函数来使用就可以了。
转载:http://dacoolbaby.iteye.com/blog/1698957