0

我收到一个错误:ORA-01791: not a SELECTed expression 当我尝试运行此查询时。当 SELECT 以逗号分隔(AH.NAME、REPLACE(A.ACTIVE_DC,',','/') 等)但使用 ||','|| 时,我可以运行它 我无法让它工作。我怎样才能让这个查询运行?谢谢!

SELECT DISTINCT
    AH.NAME_1||','||
    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
    AH.NAME_1,
    REPLACE(REPLACE(S.NAME,',','-'),'_x','-x'),
    H.FULL_NAME
4

4 回答 4

4

由于您没有尝试按顺序排列出现在串联中的字段,因此您不能只order by 1重复或重复整个字符串。您可以使用子查询:

SELECT RESULT
FROM (
    SELECT DISTINCT AH.NAME_1,
        REPLACE(REPLACE(S.NAME,',','-'),'_x','-x') AS NAME,
        H.FULL_NAME,
        AH.NAME_1||','||
        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 RESULT
    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,
    NAME,
    FULL_NAME
于 2013-06-13T15:38:52.677 回答
1

您需要将复杂的表达式从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找到它的说明。

于 2013-06-13T16:13:18.317 回答
0

如果没有字符串连接,您的查询将选择许多列,并且这些列出现在 order by 子句中。

通过串联,您仅“选择”一列,并且您使用的是不同的。您只能按出现在 select distinct 中的列排序!这样做的原因是,当您执行 select distinct 时,您可能会“合并”一堆行,其中 order by 中的子句实际上可能不同。

编辑:无论如何,这就是错误的原因;我看到其他人已经回答了你关于如何让它运行的问题。

于 2013-06-13T15:35:12.550 回答
0

我会尝试在内联视图(子查询)中执行 distinct、join 和 order by,并让连接发生在主查询中。

于 2013-06-13T15:34:42.360 回答