不是表达错误的分组(Oracle)

时间:2014-04-17 08:18:33

标签: sql oracle oracle-sqldeveloper

下面是我在oracle 12c中的sql查询。我正在使用sqlplus。它提示错误“ORA-00979:不是GROUP BY表达式”。我不知道什么时候错了。请帮忙。

PROMPT Type the speciality
ACCEPT speciality PROMPT "Speciality: "
SELECT assignment.doctor_id, doctors.name, MAX(assignment.end_assignment) AS end_contract,
(SELECT doctor_availability.end_availability
 FROM doctor_availability
 JOIN doctor_specialities ON doctor_specialities.doctor_id = doctors.id
 AND UPPER(doctor_specialities.specialities) like UPPER('%&speciality%'))
FROM assignment
JOIN doctors ON doctors.id = assignment.doctor_id
JOIN doctor_availability ON doctor_availability.doctor_id = assignment.doctor_id
WHERE doctor_availability.end_availability > 'end_contract'
GROUP BY assignment.doctor_id;

enter image description here

2 个答案:

答案 0 :(得分:3)

通常,如果使用Group by子句,则必须将所选的所有字段放在那里,而不使用aggregateat函数。在您的情况下,您选择assignment.doctor_id,doctors.name而不使用聚合函数,但在group by子句中只有assignment.doctor_id。 试试这个

SELECT assignment.doctor_id, doctors.name, MAX(assignment.end_assignment) AS end_contract,
(SELECT doctor_availability.end_availability
 FROM doctor_availability
 JOIN doctor_specialities ON doctor_specialities.doctor_id = doctors.id
 AND UPPER(doctor_specialities.specialities) like UPPER('%&speciality%'))
FROM assignment
JOIN doctors ON doctors.id = assignment.doctor_id
JOIN doctor_availability ON doctor_availability.doctor_id = assignment.doctor_id
WHERE doctor_availability.end_availability > 'end_contract'
GROUP BY assignment.doctor_id, doctors.name

答案 1 :(得分:2)

您需要在组中包含所有选择值。

PROMPT Type the speciality
ACCEPT speciality PROMPT "Speciality: "
SELECT assignment.doctor_id, doctors.name, MAX(assignment.end_assignment) AS end_contract,
(SELECT doctor_availability.end_availability
FROM doctor_availability
JOIN doctor_specialities ON doctor_specialities.doctor_id = doctors.id
AND UPPER(doctor_specialities.specialities) like UPPER('%&speciality%'))
FROM assignment
JOIN doctors ON doctors.id = assignment.doctor_id
JOIN doctor_availability ON doctor_availability.doctor_id = assignment.doctor_id
WHERE doctor_availability.end_availability > 'end_contract'
GROUP BY assignment.doctor_id, doctors.name, doctor_availability.end_availability;