【问题标题】:SQL query error: “right parenthesis missing missing” [duplicate]SQL查询错误:“右括号丢失丢失”[重复]
【发布时间】:2013-05-10 20:53:35
【问题描述】:

问题:显示与他们关联的医生数量最多的医院的 hospitalid、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));

【问题讨论】:

  • 谁能尽快帮助我
  • 未来问题的提示:请始终定义您正在使用的数据库系统(作为标签)。 SQL 只是查询语言 - 不是数据库产品 ....
  • 谢谢...使用 Oracle
  • 不要发布重复的问题。
  • 你们没有提供我正在寻找的答案

标签: sql oracle


【解决方案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)));

【讨论】:

  • 同样缺少右括号错误。
【解决方案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));

【讨论】:

    【解决方案3】:

    您不能使用AS tbltemp 为Oracle 中的表设置别名。 AS 关键字只能用于别名列,不能用于表。您可以删除 AS 关键字,或者在这种情况下,由于您不引用别名,请删除整个 AS tbltemp 部分。 Here's an SQL Fiddle.

    看起来解析器最初试图将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 Fiddle here。最里面的select 进行计数,下一级按每家医院医生人数的降序排列分组结果,最外面​​的将其限制为排名最高的。

    无论哪种方式,如果出现平局 - 两家医院的医生人数相同 - 您将获得多排。如果这不是你想要的,你需要决定如何打破平局。

    【讨论】:

      猜你喜欢
      • 2014-01-09
      • 2019-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-28
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多