提问人:Shantha Prasath 提问时间:5/16/2013 最后编辑:marc_sShantha Prasath 更新时间:5/16/2013 访问量:135
SQL 查询错误:“右括号缺失缺失”[重复]
SQL query error: “right parenthesis missing missing” [duplicate]
问:
问题:显示医院 id、hname、与它们关联的医生数量最多的医院的 htype。
表格:patient
patientid
pname
address
amount
ptype
表格hospital
hospitalid
hname
htype
表格:doctor
doctorid
dname
specialization
hospitalid
status
表格:billing
billingid
patientid
doctorid
fees
billdate
到目前为止,这就是我所拥有的:
select * from hospital where hospitalid =
(select hospitalid from doctor group by hospitalid having count ( doctorid ) =
(select max ( doctoramt ) from
(select count (doctorid) as doctoramt from doctor group by hospitalid) as tbltemp));
答:
1赞
Amit
5/16/2013
#1
尝试这个,但未测试
select * from hospital where hospitalid =
(select hospitalid from doctor group by hospitalid having count ( doctorid ) =
(select max ( doctoramt ) from
(select count (doctorid) as doctoramt from doctor group by hospitalid) as tbltemp)));
评论
0赞
Shantha Prasath
5/16/2013
同样缺少右 paranthesis 错误。
0赞
whatsupbros
5/16/2013
#2
试试这个:
SELECT h.hospitalid, h.hname, h.htype
FROM hospital h, doctor d
WHERE h.hospitalid = d.hospitalid
GROUP BY h.hospitalid, h.hname, h.htype
HAVING COUNT(*) = (SELECT MAX(a) from (SELECT COUNT(*) a from doctor group by hospitalid));
0赞
Alex Poole
5/16/2013
#3
不能用于在 Oracle 中为表设置别名。关键字只能用于列的别名,而不能用于表的别名。您可以删除关键字,或者在这种情况下,由于您没有引用别名,因此删除整个部分。 下面是一个 SQL Fiddle。AS tbltemp
AS
AS
AS tbltemp
看起来解析器最初试图解释为别名,然后不知道应该是什么意思。AS
tbltemp
无论如何,ZZa 的方法更好,但您也可以使用分析函数来避免多次击中表格:
select hospitalid, hname, htype from (
select hospitalid, hname, htype, doc_count,
rank() over (order by doc_count desc) as rn
from (
select h.hospitalid, h.hname, h.htype,
count(d.doctorid) as doc_count
from hospital h
join doctor d on d.hospitalid = h.hospitalid
group by h.hospitalid, h.hname, h.htype
)
)
where rn = 1;
另一个 SQL 小提琴在这里。最里面的进行计数,下一级按每家医院医生人数的降序对分组结果进行排名,最外层将其限制为最高级别的医生。select
无论哪种方式,如果出现平局 - 两家医院的医生数量相同 - 你会得到多排。如果这不是你想要的,你需要决定如何打破平局。
评论