0

我正在尝试在表格列上应用 sum 函数,但我收到错误消息

ORA-00934: group function is not allowed here
00934. 00000 -  "group function is not allowed here"
*Cause:    
*Action:
Error at Line: 46 Column: 5

这是查询

select LOSA_APP.app_ref_no AS "App.Ref.No.",  
   CODE_BRANCH.branch_name AS "BRANCH",
   CODE_STAFF.staff_name AS "Officer Name",
   LOSA_CUST.full_name AS "Borrower",
   LOSA_FACILITIES.fac_type AS "Facility Type",
   losa_facilities.amt_appr AS "Limit"
from losa_app LOSA_APP
...
INNER JOIN
    losa_facilities LOSA_FACILITIES
ON
    LOSA_APP.app_ref_no = LOSA_FACILITIES.app_ref_no
where 
    LOSA_APP.app_status='A' 
and 
    .....
and 
    sum(losa_facilities.amt_appr) 

第 46 行sum(losa_facilities.amt_appr)我也试过sum(losa_facilities.amt_appr) group by losa_facilities.amt_appr,但没有用。

为什么这么说group function is not allowed here ?。我做错了什么?

谢谢

编辑: - - - - - - - - - - - - - - - - - - - - - - - - --------------------------------

select LOSA_APP.app_ref_no AS "App.Ref.No.",  
   CODE_BRANCH.branch_name AS "BRANCH",
   CODE_STAFF.staff_name AS "Officer Name",
   LOSA_CUST.full_name AS "Borrower",
   LOSA_FACILITIES.fac_type AS "Facility Type",
   LOSA_FACILITIES.amt_appr AS "Limit"
from losa_app LOSA_APP
INNER JOIN
    losa_app_z LOSA_APP_Z  
ON
    losa_app.app_ref_no = losa_app_z.app_ref_no 
INNER JOIN
    code_branch CODE_BRANCH
ON
    LOSA_APP.attend_branch = CODE_BRANCH.branch_id
INNER JOIN
    code_staff CODE_STAFF
ON
    LOSA_APP.attend_staff = CODE_STAFF.staff_id
INNER JOIN
    losa_cust LOSA_CUST
ON
    LOSA_APP.app_ref_no = LOSA_CUST.app_ref_no
INNER JOIN
    losa_facilities LOSA_FACILITIES
ON
    LOSA_APP.app_ref_no = LOSA_FACILITIES.app_ref_no
where 
    LOSA_APP.app_status='A' 
and 
    trunc(sysdate) between (nvl(LOSA_APP_Z.li_dt, LOSA_APP_Z.li_collect_dt)) AND ((trunc(sysdate))) -- falling under the reporting period
and
    (trunc(sysdate) - nvl(losa_app_z.li_dt,losa_app_z.li_collect_dt)) > 90  -- select application cases at any step after entering Documentation flows.
and 
    losa_app.product_type = :productType -- Select records based on input parameter value passed in. 
and
    code_branch.branch_code1 -- Attending IHP Branch passed in from input parameter
    like
    case :inputChannel 
        when 'ABB'  Then '%0232%'
        when 'AiBB' Then '%0347%' 
    end
and
    LOSA_CUST.app_joint_t = 'P' -- Name of PS No. staff name
and 
    GROUP BY (LOSA_APP.app_ref_no, CODE_BRANCH.branch_name, CODE_STAFF.staff_name, LOSA_CUST.full_name, LOSA_FACILITIES.fac_type) 
    HAVING sum(LOSA_FACILITIES.amt_appr) > 0

错误

ORA-00936: missing expression
00936. 00000 -  "missing expression"
*Cause:    
*Action:
Error at Line: 46 Column: 5

第 46 行是GROUP BY (LOSA_APP.app_ref_no, CODE_BRANCH.branch_name,...

我也试过这个

select LOSA_APP.app_ref_no AS "App.Ref.No.",  
   CODE_BRANCH.branch_name AS "BRANCH",
   CODE_STAFF.staff_name AS "Officer Name",
   LOSA_CUST.full_name AS "Borrower",
   LOSA_FACILITIES.fac_type AS "Facility Type",
   sum(LOSA_FACILITIES.amt_appr) AS "Limit"
from losa_app LOSA_APP
INNER JOIN
....

只需在 select 子句中使用 sum,但在这种情况下,我会收到错误消息

ORA-00937: not a single-group group function
00937. 00000 -  "not a single-group group function"
*Cause:    
*Action:
4

2 回答 2

1

要使用聚合函数(如 SUM/AVERAGE 等),您需要使用GROUP BY子句对结果进行分组。要基于列的聚合过滤结果,您应该GROUP BY对选定的列执行 a,然后使用HAVING子句指定过滤器

select LOSA_APP.app_ref_no AS "App.Ref.No.",  
   CODE_BRANCH.branch_name AS "BRANCH",
   CODE_STAFF.staff_name AS "Officer Name",
   LOSA_CUST.full_name AS "Borrower",
   LOSA_FACILITIES.fac_type AS "Facility Type",
   losa_facilities.amt_appr AS "Limit"
from losa_app LOSA_APP
...
INNER JOIN
    losa_facilities LOSA_FACILITIES
ON
    LOSA_APP.app_ref_no = LOSA_FACILITIES.app_ref_no
where 
    LOSA_APP.app_status='A' 
and 
    .....
GROUP BY <all the columns in the  SELECT clause above, except those on which aggregate functions are applied>
HAVING sum(losa_facilities.amt_appr) <relational operator and value>
于 2012-11-21T06:15:11.237 回答
0

SUM() 和其他分组依据函数必须出现在 HAVING 子句中,而不是 WHERE 子句中。

于 2012-11-21T06:15:07.697 回答