SQL - 2010年仅接受过牙科检查的所有患者

时间:2010-12-20 19:26:43

标签: sql sql-server tsql sql-server-2000

我有一个T-SQL查询,它给了我2010年最近的办公室访问,这不是牙科访问。查询的相关部分是:

AND pv.PatientVisitId IN (                
    SELECT Max(pv1.PatientVisitID)            
        FROM PatientVisit pv1        
            JOIN DoctorFacility df1 ON pv1.FacilityID = df1.DoctorFacilityID
            JOIN PatientVisitResource pvr1 ON pv1.PatientVisitId = pvr1.PatientVisitId
                JOIN DoctorFacility dfr ON pvr1.ResourceId = dfr.DoctorFacilityId
        WHERE pv1.PatientProfileID = pp.PatientProfileID        
            AND pv1.Visit < '2011-01-01'    
            AND df1.ListName NOT LIKE '%Dental%'    
    )  

现在,我想翻转一下,以便为进行牙科预约的患者进行最近的办公室访问。不过,我一直在这里撞墙。任何人都可以把我打到另一边吗? : - )

4 个答案:

答案 0 :(得分:2)

可能有很多人要做到这一点,但第一种方法是将患者访问的计数(1)与患者访问的计数(1)进行比较,其中df1.listname如'%Dental%',如果这些是平等的,然后他们只去过牙医。

答案 1 :(得分:2)

线索是在不存在的地方使用

AND pv.PatientVisitId IN (                
    SELECT Max(pv1.PatientVisitID)            
        FROM PatientVisit pv1        
            JOIN DoctorFacility df1 ON pv1.FacilityID = df1.DoctorFacilityID
            JOIN PatientVisitResource pvr1 ON pv1.PatientVisitId = pvr1.PatientVisitId
                JOIN DoctorFacility dfr ON pvr1.ResourceId = dfr.DoctorFacilityId
        WHERE pv1.PatientProfileID = pp.PatientProfileID        
            AND pv1.Visit < '2011-01-01'    
            AND df1.ListName LIKE '%Dental%'    
    ) 
and not exists ( Select 1 from   PatientVisit pv1        
            JOIN DoctorFacility df1 ON pv1.FacilityID = df1.DoctorFacilityID
            JOIN PatientVisitResource pvr1 ON pv1.PatientVisitId = pvr1.PatientVisitId
                JOIN DoctorFacility dfr ON pvr1.ResourceId = dfr.DoctorFacilityId
        WHERE pv1.PatientProfileID = pp.PatientProfileID        
            AND pv1.Visit < '2011-01-01'    
            AND df1.ListName NOT LIKE '%Dental%'    
    )

答案 2 :(得分:1)

如果您使用的是SQL Server 2005或更高版本,则可以使用EXCEPT从其他查询中排除一组记录。

所以基本上写下你的大问题(给我看看今年访问过的所有病人!),然后......

...
EXCEPT
SELECT pv1.PatientVisitID        
        FROM PatientVisit pv1        
            JOIN DoctorFacility df1 ON pv1.FacilityID = df1.DoctorFacilityID
            JOIN PatientVisitResource pvr1 ON pv1.PatientVisitId = pvr1.PatientVisitId
                JOIN DoctorFacility dfr ON pvr1.ResourceId = dfr.DoctorFacilityId
        WHERE pv1.PatientProfileID = pp.PatientProfileID        
            AND pv1.Visit < '2011-01-01'    
            AND df1.ListName NOT LIKE '%Dental%'

要注意的是,您需要在两个查询中都包含相同的列(可能存在一些差异,但出于我们的目的,请确保第一个SELECTEXCEPT查询返回相同的字段。)

答案 3 :(得分:0)

AND pv.PatientVisitId IN (              
  SELECT MAX(pv1.PatientVisitID) 
  FROM PatientVisit pv1                
    JOIN DoctorFacility df1 ON pv1.FacilityID = df1.DoctorFacilityID        
    JOIN PatientVisitResource pvr1 ON pv1.PatientVisitId = pvr1.PatientVisitId        
    JOIN DoctorFacility dfr ON pvr1.ResourceId = dfr.DoctorFacilityId       
  WHERE pv1.PatientProfileID = pp.PatientProfileID                
    AND pv1.Visit < '2011-01-01'            
  HAVING SUM(CASE WHEN df1.ListName LIKE '%Dental%' THEN 1 ELSE 0 END) = COUNT(*)
)