应用求和函数时出错(此处不允许使用组函数)

时间:2012-11-21 06:10:49

标签: sql oracle11g

我正在尝试在表列上应用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:

2 个答案:

答案 0 :(得分:1)

要使用聚合函数(如SUM / AVERAGE等),您需要使用GROUP BY子句对结果进行分组。要根据列上的聚合过滤结果,您应该对所选列执行GROUP BY,然后使用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>

答案 1 :(得分:0)

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