从视图中获取最大计数
Getting max count from a view
我创建了一个视图,允许我查看医生的预约次数。
查看代码:
create view DocApps as
select d.doctorid, d.surname ||','|| d.given as "Doctor", count(*) as "Appointments"
from doctor d, appointment
where d.doctorid=appointment.doctorid
group by d.doctorid, d.surname, d.given;
我现在需要显示预约次数最多的医生,我使用了这段代码,因为它在之前的查询中有效:
select Doctor, Appointments
from docapps
where Appointments in (select max(Appointments)
from docapps);
但它返回 "Appointments":无效的标识符,当我指定它是那个时?
使用双引号 ("
) 会使表和列等对象名称区分大小写,这就是导致您出现问题的原因。只需将它们从视图声明中删除就可以了:
CREATE VIEW DocApps AS
SELECT d.doctorid, d.surname ||','|| d.given AS Doctor,
COUNT(*) AS Appointments
FROM doctor d, appointment
WHERE d.doctorid=appointment.doctorid
GROUP BY d.doctorid, d.surname, d.given;
我创建了一个视图,允许我查看医生的预约次数。
查看代码:
create view DocApps as
select d.doctorid, d.surname ||','|| d.given as "Doctor", count(*) as "Appointments"
from doctor d, appointment
where d.doctorid=appointment.doctorid
group by d.doctorid, d.surname, d.given;
我现在需要显示预约次数最多的医生,我使用了这段代码,因为它在之前的查询中有效:
select Doctor, Appointments
from docapps
where Appointments in (select max(Appointments)
from docapps);
但它返回 "Appointments":无效的标识符,当我指定它是那个时?
使用双引号 ("
) 会使表和列等对象名称区分大小写,这就是导致您出现问题的原因。只需将它们从视图声明中删除就可以了:
CREATE VIEW DocApps AS
SELECT d.doctorid, d.surname ||','|| d.given AS Doctor,
COUNT(*) AS Appointments
FROM doctor d, appointment
WHERE d.doctorid=appointment.doctorid
GROUP BY d.doctorid, d.surname, d.given;