1

我的 SQL 有点生疏,只是好奇是否有更好的方法来执行此 WHERE 子句?假设有一种方法,我确信有。它的运行速度非常快,但我只是不喜欢它,想知道它是否可以改进。

SELECT DISTINCT p.firstname              ,
            p.middlename             ,
            p.lastname               ,
            p.gender                 ,
            p.dob                    ,
            p.id      AS patientid   ,
            pr.id     AS practiceid  ,
            pr.[name] AS practicename,
            pr.parentaco             ,
            pp.encounterdate
FROM            ((aco.patients_practices patients_practices
            LEFT OUTER JOIN aco.patients p
            ON              (
                             patients_practices.patientid = p.id
                            )
            )
            LEFT OUTER JOIN aco.practices pr
            ON              (
                             patients_practices.practiceid = pr.id
                            )
            )
            INNER JOIN aco.patientpreferences pp
            ON              (
                             pp.patientid = p.id
                            )
WHERE           (
                            pr.parentaco =
                            (SELECT parentaco
                            FROM    aco.practices
                            WHERE   master_companyid = 763
                            )
            OR              pr.id =
                            (SELECT parentaco
                            FROM    aco.practices
                            WHERE   master_companyid = 763
                            )
            )
AND             pp.encounterdate IS NOT NULL
4

4 回答 4

5

您可以先从子查询中获取值并将其放入变量中。

DECLARE @P INT

SELECT @P = parentaco
FROM aco.practices
WHERE master_companyid = 763

在查询中使用变量

WHERE (pr.parentaco = @P or pr.id = @P) AND
      pp.encounterdate IS NOT NULL
于 2012-09-07T15:33:02.943 回答
1

由于在子句中引用它们的方式,LEFT OUTER JOINs 被有效地转换为s。我想你会发现这在逻辑上是等价的:INNER JOINWHERE

select distinct p.firstname, p.middlename, p.lastname,
  p.gender, p.dob, p.id as patientid,
  pr.id as practiceid, pr.[name] as practicename, pr.parentaco,
  pp.encounterdate
  from aco.patients_practices as patients_practices inner join
    aco.patients as p on p.id = patients_practices.patientid inner join
    aco.practices as pr on pr.id = patients_practices.practiceid inner join
    aco.patientpreferences as pp on pp.patientid = p.id
  where pp.encounterdate is not null and
    ( pr.parentaco = ( select parentaco from aco.practices where master_companyid = 763 ) or
    pr.id = ( select parentaco from aco.practices where master_companyid = 763 ) )

关于WHERE子句中的重复子查询:查询优化器应该识别重复并相应地处理它。您可以通过检查执行计划来确认这一点。使用 Mikael Eriksson 的建议来捕获parentaco变量应该可以解决问题。

于 2012-09-07T17:25:57.223 回答
0

为什么在 where 条件下两次使用相同的条件?您的意思是第二次使用不同的 ID 吗?

为什么不直接加入实践表,然后在没有子选择的情况下在那里做一个 where 条件?

于 2012-09-07T15:34:10.323 回答
0

这可能不是 100%,但希望能引导您进入正确的状态。

SELECT DISTINCT p.firstname          ,
            p.middlename             ,
            p.lastname               ,
            p.gender                 ,
            p.dob                    ,
            p.id      AS patientid   ,
            pr.id     AS practiceid  ,
            pr.[name] AS practicename,
            pr.parentaco             ,
            pp.encounterdate
FROM        aco.patients_practices pp
            LEFT OUTER JOIN aco.patients p
              ON  pp.patientid = p.id  
            LEFT OUTER JOIN aco.practices pr
              ON  pp.practiceid = pr.id
              AND ( pr.parentaco = pp.parentco or pr.id = pp.parentco )
WHERE    pp.encounterdate IS NOT NULL
         AND pp.master_companyid = 763
于 2012-09-07T16:50:07.073 回答