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

Sql 使用串联字段值的oracle 12c listagg,sql,oracle,Sql,Oracle,使用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

使用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 
我现在需要做的是在一条语句中使用green_代码数据,在listag函数中收集单个item_Id值中的多个green_代码值

我所做的一切都是返回成堆的“NULL”值,而不是正确地列出串联的green_代码值

从这些相同的表和八个额外的表中,有不到五十个额外的字段需要由单个SQL语句返回到视图中,因此我尽量避免将所有字段都列在GROUPBY语句中


非常感谢您提供语法帮助,将上述串联字段绿色代码收集到单个项目Id值内的listagg函数中。…

我可能不正确,因为您需要完成的内容不清楚。但使用子查询似乎是您所需要的。语法可能有误,但希望你能理解。 e、 g:


LISTAGGall_即_mess,','组内ORDER BY NULL如果这不起作用,请使用外部查询来执行,可能是函数中表达式的求值顺序不稳定。感谢您的回复。我无法得到你的建议产生任何结果。这个问题更加复杂,因为要连接的任何一个字段都可能为空,并且它们来自两个不同的表,要连接这两个表,另一个表必须位于它们之间,因为两个基表没有直接关系。如上所述,我认为我的代码正确地连接了它们,但当我试图将它们作为一个单元列出为带有item_Id的绿色_代码时,我无法得到任何有意义的结果。由于需要添加所有其他字段,它变得更加复杂。尝试在SqlFiddle上重新创建该问题这使我更接近于我尝试过的任何内容,并且我尝试了几十种opf分类语法安排。{从select mi2.store_item_id中选择t.store_item_id作为绿色_代码的grouporder中的t.store_item_id,listagggreen_代码','从select mi2.store_item_id中选择t.store_item_id,从nvl2prod2.code中选择t.store_item_id,从NVL2MIP2.serial|item|编号,从ml|store|item mi2中选择null作为绿色|代码在mi2.store\u item\u products mipp2.store\u item\u id=mipp2.store\u item\u id在mipp2.product\u id=prod2.id t;}上加入ml\u store\u item\u products mipp2
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;