Oracle Sql 查询 return 列结果为单行

Oracle Sql query to return the column result in single row

我有以下 select 查询:

select distinct(e.ENGINE_ID),
       e.ENGINE_NAME,
       os.OBJECT_STATUS,
       br.NAME,
       env.NAME
from ENGINE e,
     RATOR_MONITORING.OBJECT_STATUS os,
     BRAND_ENGINE be,
     ENVIRONMENT env,
     BRAND br
where e.ENGINE_ID = os.OBJECT_ID
  AND os.OBJECT_TYPE='ENGINE'
  AND be.ENGINE_ID = e.ENGINE_ID
  AND be.BRAND_ID = br.BRAND_ID 
  AND br.ENV_ID = env.ENV_ID  
order by decode(os.OBJECT_STATUS, 'R',1, 'Y', 2, 'G', 3, 'N',4) asc,
         UPPER(e.ENGINE_NAME) asc

以上查询return结果为:

如您所见,return 对具有相同 ENGINE_NAME,OBJECT_STATUS,NAME_1 的副本 Engine_IDNAME 列具有相同 ENGINE_ID 的不同结果。所以我想 return 单行中此类记录的结果。例如如下所述:

ENGINE_ID  ENGINE_NAME   OBJECT_STATUS  NAME      NAME_1
39         ORDER_ENGINE  G              NC,LIDL   FONIC

Oracle 的 LISTAGG-function 是您的解决方案。 http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions089.htm#SQLRF30030

select e.ENGINE_ID,
       e.ENGINE_NAME,
       os.OBJECT_STATUS,
       LISTAGG(br.NAME, ', ') WITHIN GROUP (ORDER BY br.NAME) AS NAME,
       env.NAME as NAME_1
from ENGINE e,
     RATOR_MONITORING.OBJECT_STATUS os,
     BRAND_ENGINE be,
     ENVIRONMENT env,
     BRAND br
where e.ENGINE_ID = os.OBJECT_ID
  AND os.OBJECT_TYPE='ENGINE'
  AND be.ENGINE_ID = e.ENGINE_ID
  AND be.BRAND_ID = br.BRAND_ID 
  AND br.ENV_ID = env.ENV_ID  
group by e.ENGINE_ID,
         e.ENGINE_NAME,
         os.OBJECT_STATUS,
         env.NAME
order by decode(os.OBJECT_STATUS, 'R',1, 'Y', 2, 'G', 3, 'N',4) asc,
         UPPER(e.ENGINE_NAME) asc