使用Listagg函数需要唯一值

时间:2016-10-27 19:16:28

标签: sql oracle

我有一个查询,其中我使用Listagg函数,它显示两个相同的值,并且两个值都相同,所以我想在报告中只显示一个值

SELECT LISTAGG (OB.SHIP_WITH_GROUP , ',') 
               WITHIN GROUP ( ORDER BY OB.SHIP_WITH_GROUP) 
  FROM ORDER_RELEASE ORR, 
       OB_ORDER_BASE OB , 
       SHIPMENT SH , 
       ORDER_MOVEMENT OM 
 WHERE ORR.ORDER_BASE_GID = OB.ORDER_BASE_GID 
   AND OM.SHIPMENT_GID = SH.SHIPMENT_GID 
   AND OM.ORDER_RELEASE_GID = ORR.ORDER_RELEASE_GID 
   AND SH.SHIPMENT_GID = 'ULA/SAO.5000070627'

2 个答案:

答案 0 :(得分:0)

假设没有办法摆脱重复行(通常,人们因为未能正确地进行连接并且错过了某些条件而获得重复值),您可以执行distinct内联视图中的第一步,然后执行listagg

SELECT LISTAGG (a.SHIP_WITH_GROUP , ',') 
               WITHIN GROUP ( ORDER BY a.SHIP_WITH_GROUP) 
  FROM (SELECT DISTINCT OB.SHIP_WITH_GROUP
          FROM ORDER_RELEASE ORR, 
               OB_ORDER_BASE OB , 
               SHIPMENT SH , 
               ORDER_MOVEMENT OM 
         WHERE ORR.ORDER_BASE_GID = OB.ORDER_BASE_GID 
           AND OM.SHIPMENT_GID = SH.SHIPMENT_GID 
           AND OM.ORDER_RELEASE_GID = ORR.ORDER_RELEASE_GID 
           AND SH.SHIPMENT_GID = 'ULA/SAO.5000070627') a

答案 1 :(得分:0)

或者您可以使用正则表达式替换重复值。

SELECT
    REGEXP_REPLACE(
        LISTAGG (OB.SHIP_WITH_GROUP, ',') WITHIN GROUP (ORDER BY OB.SHIP_WITH_GROUP),
        '([^,]+)(,\1)+',
        '\1'
    )
FROM
    ORDER_RELEASE ORR, 
    OB_ORDER_BASE OB , 
    SHIPMENT SH , 
    ORDER_MOVEMENT OM 
WHERE 
    ORR.ORDER_BASE_GID = OB.ORDER_BASE_GID 
    AND OM.SHIPMENT_GID = SH.SHIPMENT_GID 
    AND OM.ORDER_RELEASE_GID = ORR.ORDER_RELEASE_GID 
    AND SH.SHIPMENT_GID = 'ULA/SAO.5000070627'