简体   繁体   中英

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

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

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

Here are the tables used:

Staff

Operation

Operation_type

I don't understand your group by. Are you sure you want it?

Intuitively it looks like + 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; 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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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