我在下面选择查询:
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
以上查询将结果返回为:
正如您所看到的那样,它返回了具有相同Engine_ID
的重复ENGINE_NAME,OBJECT_STATUS,NAME_1
,并且NAME
列对于相同的ENGINE_ID
具有不同的结果。所以我想在单行中返回这些记录的结果。例如,如下所述:
ENGINE_ID ENGINE_NAME OBJECT_STATUS NAME NAME_1
39 ORDER_ENGINE G NC,LIDL FONIC
答案 0 :(得分:1)
Oracle LISTAGG
- 函数是您的解决方案。
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