4

查找在同一天拜访了同一专业的两位不同医生的患者。

示例数据库:单击此处查看 SQL Fiddle 中的示例数据脚本。

CREATE VIEW DistinctVisits AS
SELECT v.vid,v.pid,d.speciality,v.date
FROM Visits v ,Doctors d
WHERE d.did=v.did
GROUP BY v.pid,v.did,v.date;

CREATE VIEW DistinctVisits2 AS
SELECT dv.pid,dv.speciality,dv.date, COUNT(dv.vid) as countv
FROM DistinctVisits dv
GROUP BY dv.pid,dv.speciality,dv.date;

SELECT dv2.pid,dv2.speciality
FROM DistinctVisits2 dv2
WHERE dv2.countv=2;

DROP VIEW DistinctVisits;
DROP VIEW DistinctVisits2;

我如何重复相同的想法,但只针对一个大查询?另一个解决方案也不错,但请先尝试帮助我改进这个解决方案。

4

5 回答 5

7

解释:

  • 您需要查找在给定日期访问过同一专业的两位不同医生的患者列表。

  • 在此要求中,您的Patient表成为主表。让我们先查询该表。

  • 现在我们有了患者名单。我们需要得到他们访问过的医生的名单。我们不能简单地将患者表与医生表连接起来,因为没有用于映射数据的列。我们必须使用 Visits 作为中间表

  • LEFT OUTER JOIN在 Patient 和 Visits 表之间添加一个并按pid列加入。

  • 我们有病人和他们就诊的名单,但现在我们需要获取医生的信息。LEFT OUTER JOIN因此,在 Visits 和 Doctors 表之间添加另一个,并通过did列加入。

  • 我们有患者和医生就诊信息。但是,我们只需要患者的姓名、就诊医生的专业和就诊日期。因此,我们将在SELECT子句和GROUP BY子句中添加列p.pname和。除此之外,我们需要所有访问次数,但有一个问题。我们只需要 DISTINCT 计数,换句话说,我们需要他们访问过的所有唯一医生的计数。因此,如果患者在一天内两次拜访同一位医生,则应计为 1。因此,在这里添加将有所帮助。此外,关键是使用正确的列名,在这种情况下d.did代表医生。d.specialityv.dateDISTINCT

  • 我们拥有我们需要的所有数据,但我们只需要过滤在同一天拜访了两位不同医生的患者。为此,HAVING 子句可以帮助我们。当您应用 GROUP BY 时,HAVING 是合适的。我们将使用相同的 COUNT(DISTINCT d.did) 来检查计数是否仅与2的值匹配。您可以在输出中看到结果。

建议:

  • 您不必为插入到表中的每个值指定 INSERT INTO 语句。您可以在括号内将它们组合在一起,并用 . 分隔它们commas。最后一条语句应以semicolon.

  • 该查询使用LEFT OUTER JOIN. 我使用此联接来找出每位患者的所有就诊次数,即使他们从未看过医生。我只是想在形成查询时查看输出。您可以将其更改为INNER JOIN,我认为这更适合您的场景。

  • 如果您不想显示访问次数,可以将其从 SELECT 子句中删除。

演示:

单击此处查看 SQL Fiddle 中的演示。

解释中使用的脚本:

SELECT          p.pname
            ,   d.speciality 
            ,   v.date
            ,   COUNT(DISTINCT d.did) AS visitcount
FROM            Patient p
LEFT OUTER JOIN Visits v
ON              v.pid = p.pid
LEFT OUTER JOIN Doctors d
ON              d.did = v.did
GROUP BY        p.pname
            ,   d.speciality
            ,   v.date
HAVING          COUNT(DISTINCT d.did) = 2

更适合您的脚本:

SELECT      p.pname
        ,   d.speciality 
        ,   v.date
FROM        Patient p
INNER JOIN Visits v
ON          v.pid = p.pid
INNER JOIN Doctors d
ON          d.did = v.did
GROUP BY    p.pname
        ,   d.speciality
        ,   v.date
HAVING      COUNT(DISTINCT d.did) = 2

输出:

PNAME      SPECIALITY    DATE       VISITCOUNT
---------  ------------  ---------  -----------
Loch Ness  Assholes      17/9/2012      2
Loch Ness  Orthopedist   13/1/2011      2

创建表并插入脚本:

create table InsuranceCompanies  (
    cid         int,
    cname       varchar(20),
    primary key (cid)
);

create table Patient (
    pid         int,
    pname       varchar(20),
    age         int,
    cid         int,
    gender      char,
    primary     key (pid),
    constraint foreign key (cid) 
        references InsuranceCompanies (cid) 
);

create table Doctors (
    did         int ,
    dname       varchar(20),
    speciality  varchar(20),
    age         int,
    cid         int,
    primary key (did),
    constraint foreign key (cid) 
        references InsuranceCompanies (cid) 
);

create table Visits(
    vid         int,
    pid         int,
    did         int,
    date        varchar(20),
    primary key (vid),
    constraint foreign key (pid) 
        references Patient (pid) ,
    constraint foreign key (did) 
        references Doctors (did)
);

INSERT INTO InsuranceCompanies(cid, cname) VALUES
    ( 1111, 'Harel Inc' ),
    ( 2222, 'Clalit Inc' );

INSERT INTO Doctors ( did, dname, speciality, age, cid) VALUES 
    ( 100, 'Jhonny Depp',       'Heart',        42, 1111 ),
    ( 101, 'Tom Tolan',         'Assholes',     62, 1111 ),
    ( 105, 'Yom Tov',           'Assholes',     52, 1111 ),
    ( 102, 'Lauren Jaime',      'Throat',       27, 2222 ),
    ( 103, 'Gomez Flaurence',   'Legs',         37, 2222 ),
    ( 106, 'David Harpaz',      'Orthopedist',  37, 2222 ),
    ( 107, 'David Schwimmer',   'Orthopedist',  37, 2222 ),
    ( 108, 'Sammy Salut',       'Orthopedist',  37, 1111 );

INSERT INTO Patient ( pid, pname, age, cid,gender) VALUES 
    ( 200, 'Jon Gilmour',       25, 2222, 'm' ),
    ( 206, 'Bon Gilmour',       30, 2222, 'm' ),
    ( 205, 'Jon Gilmour',       22, 2222, 'm' ),
    ( 201, 'Bon Jovy',          21, 2222, 'm' ),
    ( 202, 'Loch Ness',         17, 2222, 'f' ),
    ( 203, 'Lilach Sonin',      12, 1111, 'f' ),
    ( 209, 'Lilach Dba',        34, 1111, 'f' ),
    ( 210, 'Paulina Daf',       32, 1111, 'f' ),
    ( 204, 'Gerry Jalor',       23, 1111, 'm' ),
    ( 208, 'Jerrushalem Jalor', 23, 1111, 'm' );

INSERT INTO Visits ( vid, pid, did, date) VALUES 
    ( 300, 204, 100,    '12/12/2012' ),
    ( 301, 204, 101,    '12/12/2012' ),
    ( 302, 204, 101,    '02/01/2012' ),
    ( 303, 202, 101,    '17/09/2012' ),
    ( 311, 202, 105,    '17/09/2012' ),
    ( 304, 203, 102,    '12/12/2011' ),
    ( 312, 202, 106,    '13/06/2012' ),
    ( 314, 202, 107,    '13/01/2011' ),
    ( 313, 202, 108,    '13/01/2011' ),
    ( 305, 204, 102,    '10/10/2011' ),
    ( 306, 201, 100,    '12/01/2012' ),
    ( 316, 204, 108,    '18/05/2012' ),
    ( 307, 202, 100,    '12/07/2012' ),
    ( 315, 203, 108,    '12/07/2012' ),
    ( 310, 204, 103,    '10/04/2012' ),
    ( 308, 203, 102,    '12/12/2011' ),
    ( 309, 200, 101,    '12/12/2012' );
于 2012-05-05T13:49:44.587 回答
2

您需要的是在按日期和专业分组后HAVING查找的子句。COUNT(*) = 2事实上,甚至不需要嵌套。(我还FROM用一个显式的逗号分隔子句替换了你的隐式连接JOIN,这是更受欢迎的现代语法)。

SELECT 
  v.pid,
  d.speciality,
  v.date,
  COUNT(COUNT DISTINCT d.did) AS numvisits
FROM 
  visits v
  JOIN Doctors d ON v.did = d.did
GROUP BY v.pid, d.speciality, v.date
HAVING COUNT(COUNT DISTINCT d.did) = 2
/* Note - depending on your RDBMS, you may
   be able to use the count alias as
HAVING numvisits = 2 
   MySQL allows this, for ex, but MS SQL Server doesn't and I think Oracle doesn't */

此处的SELECT列表GROUP BY应该为这 3 列的聚合组合生成患者 ID、专业、日期和访问次数。然后,该HAVING条款将其限制为仅对该组进行 2 次访问的人。

仅从中提取患者,请将其包装在子查询中:

SELECT Patients.* 
FROM Patients JOIN (
  SELECT 
    v.pid,
    d.speciality,
    v.date,
    COUNT(COUNT DISTINCT d.did) AS numvisits
  FROM 
    visits v
    JOIN Doctors d ON v.did = d.did
  GROUP BY v.pid, d.speciality, v.date
  HAVING COUNT(COUNT DISTINCT d.did) = 2
) subq ON Patients.pid = subq.pid
于 2012-05-05T13:06:59.433 回答
1

在 Siva 的回答之后有点晚了,但这是我的问题:

SELECT v.pid, d.speciality, count(DISTINCT d.did) as cnt
  FROM Visits v
  JOIN Doctors d ON v.did = d.did
 GROUP BY v.pid, d.speciality, v.date
HAVING count(DISTINCT d.did) = 2;

它产生与没有视图的初始 OP 查询完全相同的输出。

于 2012-05-05T14:04:17.130 回答
0
WITH
dv1 as
(
    SELECT v.vid, v.pid, d.speciality, v.date
    FROM Visits v, Doctors d
    WHERE d.did=v.did
    GROUP BY v.pid,v.did,v.date;
),
dv2 as
(
    SELECT dv.pid,dv.speciality,dv.date, COUNT(dv.vid) as countv
    FROM dv1
    GROUP BY dv.pid,dv.speciality,dv.date;
)
SELECT dv2.pid, dv2.speciality
FROM dv2
WHERE dv2.countv=2;
于 2012-05-05T13:07:24.730 回答
0

您可以使用嵌套的 CTE(如果您不在 mysql 上,那就是......) CTE 可以被视为即时视图,仅在语句期间存在。(注:未经测试)

WITH DistinctVisits2 AS (
    WITH DistinctVisits AS (
        SELECT v.vid,v.pid,d.speciality,v.date
        FROM Visits v
        JOIN Doctors d ON d.did=v.did
        GROUP BY v.pid,v.did,v.date
        )
    SELECT dv.pid,dv.speciality,dv.date, COUNT(dv.vid) as countv
    FROM DistinctVisits dv
    GROUP BY dv.pid,dv.speciality,dv.date
    )
SELECT dv2.pid,dv2.speciality
FROM DistinctVisits2 dv2
WHERE dv2.countv=2
    ;
于 2012-05-05T13:09:56.213 回答