找到医生人数最多的病人?

时间:2016-10-15 19:47:33

标签: mysql sql-server

我正在使用这些表:

TABLE Adm_Med:

  

Adm_ID / * ID入院

     

Med_ID

     

Doc_ID / * ID出席相应Adm_ID患者的医生

表格入场费:

  

Adm_ID

     

Pat_ID / * ID患者

     

Date_Admission

     

Date_Discharge

TABLE患者:

  

Pat_ID

     

Pat_Lastname

     

Pat_Firstname

     

生日

我希望查询返回类似的内容:

  

Adm_ID | Pat_Lastname | Pat_Firstname | Numberattentions

但我有两个疑问:

  

1)与我写的脚本(见下文)相比,有没有   效率更高的结果如上所述?我觉得我写了很多。

     

2)到目前为止,我已经能够获得前3列。我没有   想法如何放置Numberattentions列。我一直试图这样做,但每次都会出错。

我的尝试(这里我逐步解释了我已经做过的事情,但最后是完整的查询。如果你不想阅读所有内容):

我计算了在给定Adm_ID

中就诊的医生人数
SELECT Adm_ID, COUNT(doc_id) as numdoc
FROM Adm_Med
GROUP BY Adm_ID

然后拿了最大值。那些关注

SELECT MAX(numdoc)
FROM (SELECT Adm_ID, COUNT(doc_id) as numdoc
FROM Adm_Med
GROUP BY Adm_ID) temp

将其与相应的Adm_ID

相匹配
SELECT Adm_ID
FROM Adm_Med
GROUP BY Adm_ID
HAVING COUNT(doc_id) IN (SELECT MAX(numdoc)
                         FROM (SELECT Adm_ID, COUNT(doc_id) as numdoc
                               FROM Adm_Med
                               GROUP BY Adm_ID) temp)

在表AdmissionPatient之间形成内部联接,通过WHERE ... IN子句匹配Admission.Adm_ID和注意Adm_ID最多的医生(这是我得到的最终剧本):

SELECT Adm_ID, Pat_Lastname, Pat_Firstname
FROM Admission a 
INNER JOIN Patient p
    ON a.Pat_ID=p.Pat_ID
    WHERE Adm_ID IN (SELECT Adm_ID
                    FROM Adm_Med
                    GROUP BY Adm_ID
                    HAVING COUNT(doc_id) IN 
                               (SELECT MAX(numdoc)
                                FROM (SELECT Adm_ID, COUNT(doc_id) as numdoc
                                FROM Adm_Med
                                GROUP BY Adm_ID) temp))

1 个答案:

答案 0 :(得分:0)

由于您之前的问题与MySQL有关,我猜这个也使用MySQL。

因此,MySQL遗憾地不支持Common Table Expressions(WITH子句)。哪个允许重复使用查询来计算最大值

所以下面的sql应该返回我认为你正在寻找的东西。

但是i.m.h.o.,它太罗嗦了。 我身上的高尔夫球手正在为它哭泣。所以我很想知道是否有人能找到更简洁的方法。

select q1.Adm_ID, p.Pat_Firstname, p.Pat_Lastname, q1.TotalDoctors as Numberattentions
from (
    select m.Adm_ID, a.Pat_ID, count(m.Doc_ID) as TotalDoctors
    from Admission a
    join Adm_Med m on (a.Adm_ID = m.Adm_ID)
    group by m.Adm_ID, a.Pat_ID
) q1
join (
    select Pat_ID, max(TotalDoctors) as MaxTotalDoctors
    from (
      select a.Pat_ID, count(m.Doc_ID) as TotalDoctors
      from Admission a
      join Adm_Med m on (a.Adm_ID = m.Adm_ID)
      group by m.Adm_ID, a.Pat_ID
    ) q0
    group by Pat_ID
) q2
on (q1.Pat_ID = q2.Pat_ID and q1.TotalDoctors = q2.MaxTotalDoctors)
left join Patient p on (q1.Pat_ID = p.Pat_ID);

一些测试数据:

create table Adm_Med (Adm_ID int, Med_ID int, Doc_ID int);
insert into Adm_Med (Adm_ID, Med_ID, Doc_ID) values 
(101,201,301),(101,202,302),(102,203,301),(102,204,302),(102,205,303),
(103,201,301),(103,202,302),(104,203,301),(104,204,302),(104,205,303);

create table Admission (Adm_ID int, Pat_ID int, Date_Admission date, Date_Discharge date);
insert into Admission (Adm_ID, Pat_ID, Date_Admission, Date_Discharge) values
(101,401,'2016-01-01','2016-02-01'),(102,401,'2016-03-01','2016-04-01'),
(103,402,'2016-01-01','2016-02-01'),(104,402,'2016-03-01','2016-04-01');

create table Patient (Pat_ID int, Pat_Firstname varchar(30), Pat_Lastname varchar(30), Birthdate date);
insert into Patient (Pat_ID, Pat_Firstname, Pat_Lastname, Birthdate) values 
(401,'John','Doe','1941-05-03'),(402,'Jane','Doe','1942-06-04');

只有1名患者,可以使用更短的SQL:

select m.Adm_ID, p.Pat_Firstname, p.Pat_Lastname, count(m.Doc_ID) as Numberattentions
from Admission a
join Adm_Med m on (a.Adm_ID = m.Adm_ID)
join Patient p on (a.Pat_ID = p.Pat_ID)
where p.Pat_ID = 402
group by m.Adm_ID, a.Pat_ID
order by Numberattentions desc
limit 1;