我正在尝试编写一个查询来划分两个 SQL 语句,但它显示了我
ERROR: column "temp.missed" must appear in the GROUP BY clause or be used in
an aggregate function
SQL state: 42803
虽然当我执行 group by temp.missed 时,它似乎有效,但由于多个 group by 显示了错误的结果。
我在 PostgreSQL 中有以下表格
create table test.appointments (
appointment_id serial
, patient_id integer references test.patients(id)
, doctor_id integer references test.doctors(id)
, appointment_time timestamp
, appointment_status enum('scheduled','completed','missed')
);
create table test.visits (
visit_id serial
, patient_id integer references test.patients(id)
, doctor_id integer references test.doctors(id)
, walk_in_visit boolean
, arrival_time timestamp
, seen_time timestamp
);
我编写了以下查询来查找错过约会率(错过约会/总约会),但它显示了上述错误。
select tem.doctor_id, (temp.missed::float/tem.total) as ratio from
(select doctor_id, count(appointment_status) as missed from appointments
where appointment_status='missed' group by doctor_id)as temp
join (select doctor_id, count(appointment_status) as total from
appointments group by doctor_id) as tem on temp.doctor_id =
tem.doctor_id group by tem.doctor_id;