ORA-00904 "Invalid identifier” for an identifier in a group by clause

前端 未结 2 1566
别那么骄傲
别那么骄傲 2020-12-11 07:47

with compiere database i am trying ro show

between two dates.

column:

designation

qty sale (depot) client sales

qty-sale

相关标签:
2条回答
  • 2020-12-11 08:15

    The problem is (at least) in the group by clause. Oracle does not allow column aliases in group by. You can readily fix this with a subquery:

    select t.*
    from (select . . .
          from . . .
          where . . .
         ) t
    group by . . . 
    order by . . . ;
    

    In other words, put everything but the group by and order by in a subquery. Then put those clauses in the outer query.

    0 讨论(0)
  • 2020-12-11 08:22

    You can't refer to a column alias in the same level of SQL, except in the order by clause.

    From the documentation (emphasis added):

    You can use a column alias, c_alias, to label the immediately preceding expression in the select list so that the column is displayed with a new heading. The alias effectively renames the select list item for the duration of the query. The alias can be used in the ORDER BY clause, but not other clauses in the query.

    When you refer to QTYLIV in the GROUP BY cluase the select list hasn't been evaluated yet and the alias doesn't exist. This is just how the query is parsed and executed.

    When you have complicated expressions in the select list it's often simplest to wrap that in an outer select and do the grouping afterwards:

    SELECT *
    FROM (
      SELECT p.name AS design,
        p.M_PRODUCT_CATEGORY_ID,
        il.PRICEACTUAL   AS price,
        bp.C_BPARTNER_ID AS idpartner,
        CASE
      ...
        (SELECT qtyinvoiced
        FROM C_InvoiceLine il
        WHERE bp.ISCUSTOMER ='Y'
        AND bp.C_BPARTNER_ID= 18888
        )               AS qtyliv,
      ...
        i.DATEINVOICED AS dat
      FROM C_InvoiceLine il
      INNER JOIN M_PRODUCT p
      ...
      ON (oi.c_location_id=loc2.c_location_id)
        --WHERE i.DateInvoiced BETWEEN $P{Date1} AND $P{Date2}
        --AND
        --i.DocStatus in ('CO','CL')
        --AND i.IsSoTrx = 'Y'
        --AND   p.isstocked='Y'
    )
    GROUP BY name ,
      M_PRODUCT_CATEGORY_ID,
      QTYINVOICED,
      PRICEACTUAL,
    ...
      qtyliv,
      qtydepot
    ORDER BY name ,
      dateinvoiced ;
    

    Notice that you don't use the original table aliases in the GROUP BY or ORDER BY clauses in the outer select, as those are no longer in scope.

    0 讨论(0)
提交回复
热议问题