您需要将复杂的表达式从select
一个部分放置到另一个部分,order by
或者相反将REPLACE(REPLACE(S.NAME,',','-'),'_x','-x')
表达式添加到选择列表中:
SELECT DISTINCT
AH.NAME||','||
REPLACE(A.ACTIVE_DC,',','/')||','||
REPLACE(A.PASSIVE_DC,',','/')||','||
REPLACE(H.ENVIRONMENT,',','/')||','||
REPLACE(REPLACE(S.NAME,',','-'),'_x','-x')||','||
H.FULL_NAME||','||
H.PRIMARY_IP||','||
H.COMPLIANCE||','||
H.OS
FROM
HOST H
FULL OUTER JOIN
APP_HOST AH ON
AH.ID_2 = H.ID
FULL OUTER JOIN
HOST_SVR HS ON
HS.ID_1 = H.ID
FULL OUTER JOIN
APP A ON
AH.ID_1 = A.ID
FULL OUTER JOIN
SVR S ON
HS.ID_2 = S.ID
WHERE S.NAME IS NOT NULL
ORDER BY
-- same as selected
AH.NAME||','||
REPLACE(A.ACTIVE_DC,',','/')||','||
REPLACE(A.PASSIVE_DC,',','/')||','||
REPLACE(H.ENVIRONMENT,',','/')||','||
REPLACE(REPLACE(S.NAME,',','-'),'_x','-x')||','||
H.FULL_NAME||','||
H.PRIMARY_IP||','||
H.COMPLIANCE||','||
H.OS
从任务的角度来看,上面的查询会产生错误的结果,因为排序顺序与初始查询不同。
所以使用第二个选项:
SELECT DISTINCT
AH.NAME||','||
REPLACE(A.ACTIVE_DC,',','/')||','||
REPLACE(A.PASSIVE_DC,',','/')||','||
REPLACE(H.ENVIRONMENT,',','/')||','||
REPLACE(REPLACE(S.NAME,',','-'),'_x','-x')||','||
H.FULL_NAME||','||
H.PRIMARY_IP||','||
H.COMPLIANCE||','||
H.OS,
-- next from order by
AH.NAME_1,
REPLACE(REPLACE(S.NAME,',','-'),'_x','-x'),
H.FULL_NAME
FROM
HOST H
FULL OUTER JOIN
APP_HOST AH ON
AH.ID_2 = H.ID
FULL OUTER JOIN
HOST_SVR HS ON
HS.ID_1 = H.ID
FULL OUTER JOIN
APP A ON
AH.ID_1 = A.ID
FULL OUTER JOIN
SVR S ON
HS.ID_2 = S.ID
WHERE S.NAME IS NOT NULL
ORDER BY
AH.NAME_1,
REPLACE(REPLACE(S.NAME,',','-'),'_x','-x'),
H.FULL_NAME
第二个变体有效,因为所有order by
表达式都包含在构造字符串中,并且不能产生比初始变体更多的不同值。
但是,如果您想生成只有一个字段的结果集,那么您必须使用子查询分两步执行此操作:第一步不同,第二步 - 排序没有不同:
select
full_string
from (
SELECT DISTINCT
(
AH.NAME||','||
REPLACE(A.ACTIVE_DC,',','/')||','||
REPLACE(A.PASSIVE_DC,',','/')||','||
REPLACE(H.ENVIRONMENT,',','/')||','||
REPLACE(REPLACE(S.NAME,',','-'),'_x','-x')||','||
H.FULL_NAME||','||
H.PRIMARY_IP||','||
H.COMPLIANCE||','||
H.OS
) as full_string,
-- next from order by
AH.NAME_1,
REPLACE(REPLACE(S.NAME,',','-'),'_x','-x') S_NAME_REPLACE,
H.FULL_NAME,
FROM
HOST H
FULL OUTER JOIN
APP_HOST AH ON
AH.ID_2 = H.ID
FULL OUTER JOIN
HOST_SVR HS ON
HS.ID_1 = H.ID
FULL OUTER JOIN
APP A ON
AH.ID_1 = A.ID
FULL OUTER JOIN
SVR S ON
HS.ID_2 = S.ID
WHERE S.NAME IS NOT NULL
)
ORDER BY
NAME_1,
S_NAME_REPLACE,
FULL_NAME
它之所以有效,是因为最终查询中没有聚合表达式。
可以在这个 SQLFiddle找到它的说明。