简体   繁体   English

Oracle SQL:ORA-00937:不是单组组功能

[英]Oracle SQL: ORA-00937: not a single-group group function

Im trying to calculate the amount of money earned by the surgeon with the ID 118 in 2016 我正在尝试计算2016年ID为118的外科医生的收入

By using the code : 通过使用代码:

    select (sum(count(*) * professional_fee)) AS 
    "Total amount earned by 118"
    from operation, operation_type, staff
    where operation.actual_op = operation_type.op_code
    and staff.person_id = operation.surgeon
    and surgeon = 118
    and extract(YEAR from op_date) = 2016
    group by professional_fee;

I can get the correct result of 9600 我可以得到9600的正确结果

However when I add 但是当我添加

    select (sum(count(*) * professional_fee) **+ annual_basic_salary**) AS 
    "Total amount earned by 118"
    from operation, operation_type, staff
    where operation.actual_op = operation_type.op_code
    and staff.person_id = operation.surgeon
    and surgeon = 118
    and extract(YEAR from op_date) = 2016
    group by professional_fee, **annual_basic_salary**;

I get the error: ORA-00937: not a single-group group function 我收到错误消息:ORA-00937:不是单组分组功能

Here are the tables used: 以下是使用的表:

Staff 员工

Operation 手术

Operation_type Operation_type

I don't understand your group by. 我不理解你的团体。 Are you sure you want it? 确定要吗?

Intuitively it looks like + max(annual_basic_salary) 直观地看起来像+ max(annual_basic_salary)

No group by: 没有分组依据:

select (sum(professional_fee))+max(annual_basic_salary) AS 
    "Total amount earned by 118"
    from operation, operation_type, staff
    where operation.actual_op = operation_type.op_code
    and staff.person_id = operation.surgeon
    and surgeon = 118
    and  extract(year from o.op_date) = 2016

Would something like this do? 这样会做吗? (I shortened your tables; didn't feel like typing too much.) (我缩短了您的表;不想输入太多。)

I didn't fix your query, but rather wrote a new one. 我没有解决您的查询,而是写了一个新查询。 I don't know why you used COUNT function; 我不知道为什么要使用COUNT函数; what did you count? 你算什么? If you do want to use your code anyway, have a look at what @LoztInSpace wrote & my comment under that answer. 如果仍然要使用代码,请查看@LoztInSpace编写的内容以及该答案下的我的评论。

Also, I tried to properly join tables and use table aliases for all columns. 另外,我尝试正确地联接表并为所有列使用表别名。 I suggest you to do the same in the future. 我建议您将来也这样做。 Without it, it is difficult to guess which table those columns belong to. 没有它,很难猜测这些列属于哪个表。

SQL> with
  2  operation (actual_op, surgeon, op_date) as
  3    (select 'LO', 118, date '2016-05-06' from dual union all
  4     select 'HT', 118, date '2016-05-06' from dual union all
  5     select 'TS', 118, date '2016-05-07' from dual union all
  6     select 'TS', 111, date '2016-01-01' from dual
  7    ),
  8  operation_type (op_code, operation_name, professional_fee) as
  9    (select 'LO', 'Lobotomy'        , 1050 from dual union all
 10     select 'HT', 'Heart Transplant', 7500 from dual union all
 11     select 'TS', 'Tonsillectomy'   , 1050 from dual union all
 12     select 'AP', 'Appendicectomy', 750 from dual
 13    ),
 14  staff (person_id, annual_basic_salary) as
 15    (select 118, 20000 from dual union all
 16     select 111,  8000 from dual
 17    )
 18  select sum(t.professional_fee) + s.annual_basic_salary as result
 19  from operation_type t join operation o on o.actual_op = t.op_code
 20  join staff s on s.person_id = o.surgeon
 21  where s.person_id = 118
 22    and extract(year from o.op_date) = 2016
 23  group by s.annual_basic_salary;

    RESULT
----------
     29600

SQL>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM