代码之家  ›  专栏  ›  技术社区  ›  Renaud is Not Bill Gates

在Oracle中引用别名子查询

  •  0
  • Renaud is Not Bill Gates  · 技术社区  · 6 年前

    我有以下疑问:

    select
            A.A_ID,
            B.Lib,
            A.Lib,
            C.Lib,
            (SELECT count(*) FROM X WHERE A.A_ID = X.A_ID) AS countX,
            (SELECT count(*) FROM Y WHERE A.A_ID = Y.A_ID) AS countY,
            (SELECT count(*) FROM Z WHERE A.A_ID = Z.A_ID) AS countZ
        from
            A
        left outer join
            C
                on A.C_ID=C.C_ID
        left outer join
            B
                on A.B_ID=B.B_ID
        WHERE
            countX = 2 AND countY = 3
        ORDER BY 
            countZ DESC;
    

    但这给了我以下错误:

    SQL错误[904][42000]:ORA-00904:“列5\u 0”:标识非 缬氨酸

    1 回复  |  直到 6 年前
        1
  •  1
  •   Zaynul Abadin Tuhin    6 年前

    试着像下面,我想你错过了类型组由它将是秩序

    with cte as
    (
    
    select
            A.A_ID,
            B.Lib,
            A.Lib,
            C.Lib,
            (SELECT count(*) FROM X WHERE A.A_ID = X.A_ID) AS countX,
            (SELECT count(*) FROM Y WHERE A.A_ID = Y.A_ID) AS countY,
            (SELECT count(*) FROM Z WHERE A.A_ID = Z.A_ID) AS countZ
        from
            A
        left outer join
            C
                on A.C_ID=C.C_ID
        left outer join
            B
                on A.B_ID=B.B_ID
    ) select * from cte where countX = 2 AND countY = 3
        Order BY 
        countZ DESC