4

给定:

InsuranceCompanies(cid,姓名,电话,地址)

医生(、姓名、专业、地址、电话、年龄、cid)

患者(pid、姓名、地址、电话、年龄、性别、cid)

访问次数(vid、did、pid、date、description)

在哪里:

cid - Insurance Company code
did - doctor code
pid - patient code
vid - code of visit

还有一个任务:查找今年访问次数少于所有医生平均访问次数的医生(做了,姓名)。

我的尝试是:

SELECT  D.did, D. name
FROM    Doctor D,Visit V
WHERE   V.did = D.did   and   D.did = CV.did   and   CV.visits <
                (SELECT AVG ( CV.visits) 
                 FROM   (SELECT V1.did AS did,COUNT(V1.vid) AS visits
                         FROM   Visit V1
                         WHERE  V1.date LIKE '%2012' 
                         GROUP BY   V1.did) AS CV)
4

1 回答 1

2

非常感谢 Bridge 谁分享了有史以来最漂亮和用户友好的 SQL 命令可视化器!

数据库示例: http ://sqlfiddle.com/#!2/e85c7/3

使用视图的解决方案:

CREATE VIEW  ThisYear AS 
    SELECT v.pid,v.vid,v.did
    FROM Visits v
     WHERE v.date LIKE '%2012';

CREATE VIEW DoctorsVisitCount AS
    SELECT v.did, COUNT(v.vid) as c
    FROM ThisYear v
    GROUP BY v.did;

SELECT DISTINCT d.did,d.dname,dvc.c
FROM Doctors d,DoctorsVisitCount dvc
WHERE dvc.c < (SELECT AVG(dvc.c)
               FROM DoctorsVisitCount dvc);
于 2012-04-25T09:32:13.100 回答