简体   繁体   English

oracle 12c listagg使用串联的字段值

[英]oracle 12c listagg using concatenated field values

Using NVL2 to concatenate two fields, the following returns correct data. 使用NVL2连接两个字段,以下代码将返回正确的数据。

 select mi2.item_id,
     nvl2(mipp2.serial_number, mipp2.serial_number||'-', null)
     ||nvl2(prod2.code, '' ||prod2.code, null) as green_code
           from store_item mi2
           join store_item_products mipp2
             on mi2.store_item_id = mipp2.store_item_id
           join product prod2 
             on mipp2.product_id = prod2.id 

What I now need to do is use the green_code data, all within one statement, within a listagg function to gather multiple green_code values within single item_Id values 我现在需要做的是在listagg函数中的一条语句中全部使用green_code数据,以在单个item_Id值内收集多个green_code值

everything I have tried is returning piles of 'NULL' values and not properly listing the concatenated green_code values. 我尝试过的一切都是返回一堆'NULL'值,并且没有正确列出串联的green_code值。

There are just under fifty additional fields, from these same and eight additional tables, that need to be returned by the single SQL statement (into a view), so I am trying to stay away from having to list all fields into the group by statement. 这些相同的表和8个其他表中有不到50个其他字段需要由单个SQL语句(返回到视图中)返回,因此我试图避免将所有字段都列为by语句。

Any help with the syntax to gather the above listed concatenated field (green_code) into a listagg function within single item_Id values will be greatly appreciated.... 将在单个item_Id值内将上面列出的串联字段(green_code)收集到listagg函数中的语法方面的任何帮助,将不胜感激。

I may not be correct because what you need to fulfill is not clear. 我可能不正确,因为您需要实现的目标尚不清楚。 But using a sub query is all what you need it seems. 但是,看起来似乎只需要使用子查询即可。 Syntax may be wrong but hope you can get the idea. 语法可能是错误的,但希望您能理解。 eg : 例如:

SELECT item_id, listagg(green_code, ',') WITHIN GROUP (ORDER BY green_code) as green_code_list 
FROM (select mi2.item_id item_id,
     nvl2(mipp2.serial_number, mipp2.serial_number||'-', null)
     ||nvl2(prod2.code, '' ||prod2.code, null) as green_code
           from store_item mi2
           join store_item_products mipp2
             on mi2.store_item_id = mipp2.store_item_id
           join product prod2 
             on mipp2.product_id = prod2.id )
GROUP BY item_id;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM