select语句中ORACLE SUBQUERY中的标识符错误无效

时间:2015-07-14 05:56:14

标签: oracle subquery

尝试执行以下查询,我面临无效的标识符异常。

QUERY:

SELECT bill_start_date                        AS "Bill Start Date", 
       bill_end_date                          AS "Bill End Date", 
       usage_category                         AS "Usage Category", 
       SUM(monthly_charges)                   AS "Monthly Rental Charges", 
       SUM(one_time_charges)                  AS "One Time Charges", 
       SUM(other_charges)                     AS "Other Charges", 
       SUM(no_of_users)                       AS Total_Number_of_Users, 
       SUM(monthly_charges + one_time_charges 
           + other_charges)                   AS "Total Revenue from the Users", 
       SUM(( monthly_charges + one_time_charges 
             + other_charges ) / no_of_users) AS "ARPU" 
FROM   (SELECT b.bill_start_date AS "Bill_Start_Date", 
               b.bill_end_date   AS "Bill_End_Date", 
               ( CASE 
                   WHEN bb.charge_type = '/item/cycle_forward' THEN 
                   'Monthly_Rental_Charges' 
                   WHEN bb.charge_type = '/item/purchase' THEN 
                   'One_Time_Charges' 
                   WHEN bb.charge_type LIKE '/item/%tax%' THEN 'Other_Charges' 
                   ELSE 'Unknown_Charges' 
                 END )           AS "Usage_Category", 
               SUM(CASE 
                     WHEN bb.charge_type = '/item/cycle_forward' THEN 
                     bb.amt_excl_tax 
                     ELSE 0 
                   END)          AS "Monthly_Charges", 
               SUM(CASE 
                     WHEN bb.charge_type = '/item/purchase' THEN bb.amt_excl_tax 
                     ELSE 0 
                   END)          AS "One_Time_Charges", 
               SUM(CASE 
                     WHEN bb.charge_type LIKE '/item/%tax%' THEN bb.amt_excl_tax 
                     ELSE 0 
                   END)          AS "Other_Charges", 
               Count(1)          AS "no_of_users", 
               bb.ban_id 
        FROM   tib_billsnap.bs_bill b, 
               tib_billsnap.bs_billlines bb 
        WHERE  b.ban_id = bb.ban_id 
        GROUP  BY b.bill_start_date, 
                  b.bill_end_date, 
                  bb.ban_id) ARPU 
GROUP  BY bill_start_date, 
          bill_end_date, 
          usage_category 

ERROR:

ORA-00904: "USAGE_CATEGORY": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Error at Line: 28 Column: 14

3 个答案:

答案 0 :(得分:0)

没有名称USAGE_CATEGORY的字段。您的GROUP BY和选择应该有"Usage Category",因为您已经按照这种方式别名

答案 1 :(得分:0)

在子查询中,您将创建区分大小写的标识符"Usage_Category"。创建区分大小写的标识符通常是个坏主意,因为这意味着每次引用列时都需要使用区分大小写的标识符。在这种情况下,您需要在selectgroup by中使用区分大小写的标识符。

SELECT ...
       "Usage_Category" as "Usage_Category",
       ...
  FROM (...) arpu
 GROUP BY ...
          "Usage_Category"

当然,您需要对所有区分大小写的标识符("Bill_Start_Date""Bill_End_Date"等)执行相同的操作。

通常,首选方法是首先不创建区分大小写的标识符,或者如果前端应用程序使用区分大小写的标识符来生成表的标头,则至少将它们限制为最外层查询。在这种情况下,您的子查询将具有类似

的内容
            ( CASE 
               WHEN bb.charge_type = '/item/cycle_forward' THEN 
               'Monthly_Rental_Charges' 
               WHEN bb.charge_type = '/item/purchase' THEN 
               'One_Time_Charges' 
               WHEN bb.charge_type LIKE '/item/%tax%' THEN 'Other_Charges' 
               ELSE 'Unknown_Charges' 
             END )           AS Usage_Category

答案 2 :(得分:0)

在子查询中缺少Usage_Category别名的group by子句。 您应该在第一个(内部)组中添加bb.charge_type:

group by b.bill_start_date, b.bill_end_date, bb.BAN_ID, bb.charge_type