使用 MAX 和 COUNT 从 3 个不同的表中生成报告?

发布于 2024-12-19 06:10:52 字数 573 浏览 2 评论 0原文

我有一个包含患者、医生、橱柜和就诊信息的数据库。

患者(id_pac、FirstName、LastName)

医生(id_med、FirstName、LastName、专业)

内阁(id_cab、姓名)

可视化(id_viz、Date_viz) , Medic_id,Patient_id, Cabinet_id)

一名医生可以在不同的日期、在不同的柜子上拜访不同的患者。

我需要在一份报告中找到(使用子查询或连接?,但不使用中间表或视图):

  1. 包含医生的完整姓名的一列:CONCAT(FirstName,' ', LastName) (DISTINCT),
  2. 一列包含医生就诊次数最多的内阁名称 和 3.一栏显示该柜子的访问次数。

我试图弄清楚如何获取这些信息,但我所能拥有的只是 他就诊的每个柜子的医生名单,以及每个柜子的总就诊次数:((

I have a database with patients, doctors, cabinets and visits.

Patients (id_pac, FirstName, LastName)

Doctors (id_med, FirstName, LastName, Speciality)

Cabinets (id_cab, Name)

Vizits (id_viz, Date_viz, Medic_id,Patient_id, Cabinet_id)

One doctor can make visits on different patients, on different cabinets, on different dates.

I need to find out (with a subquery or with a join ? , but NOT with intermediary tables or views), in one report :

  1. one column with the doctor's complete name : CONCAT(FirstName,' ',LastName) (DISTINCT),
  2. one column with the cabinet name in which the doctor was having the biggest number of visits
    and
    3.one column with the number of visits for this cabinet.

I have tried to figure out how can i get this information, but all what I can have is
the list of doctors with every cabinets in which he was having visits, with the total number of visits / cabinet :((

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2024-12-26 06:10:52
SELECT CONCAT(d.FirstName,' ',d.LastName) AS CompleteName
     , c.Name                             AS CabinetName  
     , dv.MaxVisits                       AS MaxVisits
FROM Doctors AS d
  LEFT JOIN
    ( SELECT Medic_id
           , MAX(NumVisits) AS MaxVisits
      FROM
        ( SELECT Medic_id
               , Cabinet_id
               , COUNT(*) AS NumVisits
          FROM Vizits AS v
          GROUP BY Medic_id
                 , Cabinet_id
        ) AS grp
      GROUP BY Medic_id
    ) AS dv
    ON dv.Medic_id = d.id_med
  LEFT JOIN
    ( SELECT Medic_id
           , Cabinet_id
           , COUNT(*) AS NumVisits
      FROM Vizits AS v
      GROUP BY Medic_id
             , Cabinet_id
    ) AS mcv
    ON  mcv.Medic_id = dv.Medic_id
    AND mcv.NumVisits = dv.MaxVisits
  LEFT JOIN Cabinets AS c
      ON c.id_cab = mcv.Cabinet_id
SELECT CONCAT(d.FirstName,' ',d.LastName) AS CompleteName
     , c.Name                             AS CabinetName  
     , dv.MaxVisits                       AS MaxVisits
FROM Doctors AS d
  LEFT JOIN
    ( SELECT Medic_id
           , MAX(NumVisits) AS MaxVisits
      FROM
        ( SELECT Medic_id
               , Cabinet_id
               , COUNT(*) AS NumVisits
          FROM Vizits AS v
          GROUP BY Medic_id
                 , Cabinet_id
        ) AS grp
      GROUP BY Medic_id
    ) AS dv
    ON dv.Medic_id = d.id_med
  LEFT JOIN
    ( SELECT Medic_id
           , Cabinet_id
           , COUNT(*) AS NumVisits
      FROM Vizits AS v
      GROUP BY Medic_id
             , Cabinet_id
    ) AS mcv
    ON  mcv.Medic_id = dv.Medic_id
    AND mcv.NumVisits = dv.MaxVisits
  LEFT JOIN Cabinets AS c
      ON c.id_cab = mcv.Cabinet_id
那小子欠揍 2024-12-26 06:10:52

我试图更好地理解这个子查询,我找到了另一种方法(我认为是一种简单的方法)。代码是:

SELECT
CONCAT(me.Nume,' ',me.Prenume) AS Medic,
x.Denumire,
MAX(x.nrviz) AS maxviz
FROM (
        SELECT DISTINCT
            v.Medic_id,
            c.Denumire,
            COUNT(v.id_viz) AS nrviz
        FROM
            vizite v
        LEFT JOIN
            cabinete c
        ON(v.Cabinet_id=c.id_cab)
        GROUP BY
            v.Medic_id,
            v.Cabinet_id
        ORDER BY
            nrviz DESC) as x
LEFT JOIN
 medici me
ON (x.Medic_id=me.id_med)
LEFT JOIN
 cabinete ca
ON (x.Denumire=ca.Denumire)
GROUP BY
Medic
ORDER BY
    Medic,  
    maxviz DESC

@ypercube:如果您发现一些差异,请告诉我,因为了解我是否忘记或逃避某些内容很重要。

I was trying to better understand this subquery and I found an another way (a simple one i believe). The code is :

SELECT
CONCAT(me.Nume,' ',me.Prenume) AS Medic,
x.Denumire,
MAX(x.nrviz) AS maxviz
FROM (
        SELECT DISTINCT
            v.Medic_id,
            c.Denumire,
            COUNT(v.id_viz) AS nrviz
        FROM
            vizite v
        LEFT JOIN
            cabinete c
        ON(v.Cabinet_id=c.id_cab)
        GROUP BY
            v.Medic_id,
            v.Cabinet_id
        ORDER BY
            nrviz DESC) as x
LEFT JOIN
 medici me
ON (x.Medic_id=me.id_med)
LEFT JOIN
 cabinete ca
ON (x.Denumire=ca.Denumire)
GROUP BY
Medic
ORDER BY
    Medic,  
    maxviz DESC

@ypercube : Please let me know if you see some differences , because it's important to see if I forgot or escape something.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文