2012-12-28 181 views
0

首先,SQL是由服務器動態處理的,因此我的WHERE子句中的某些項目對您來說可能很奇怪,請忽略這些因爲它們不是問題。SQL Server 2008 - 需要更新SQL幫助

每我的客戶的要求,他們需要UNION其他兩個條件我的更新報告(A/患者無一患者就診 /)和(/ 患者無預約 /)。我需要幫助將這兩個子集的患者添加到我的最終更新查詢中。在目前的狀態下,它只會增加#Temp患者,我需要納入額外的患者。

任何幫助非常感謝。

我現在的SQL更新 -

DECLARE @Inactive INT 
DECLARE @Active INT 
DECLARE @PatientProfileId INT 

SELECT 
    @Inactive = MedlistsId 
FROM 
    Medlists 
WHERE 
    TableName = 'PatientProfileStatus' 
    AND Code = 'I' 

SELECT 
    @Active = MedlistsId 
FROM 
    Medlists 
WHERE 
    TableName = 'PatientProfileStatus' 
    AND Code = 'A' 

CREATE TABLE #Temp 
    (
     PatientName VARCHAR(120) , 
     PatientProfileId INT , 
     RecentId INT , 
     Recent DATETIME 
    ) 

INSERT INTO #Temp 
     SELECT 
      dbo.FormatName(pp.Prefix , pp.First , pp.Middle , pp.Last , pp.Suffix) AS Name , 
      pp.PatientProfileId , 
      MAX(pv.PatientVisitId) AS RecentId , 
      MAX(pv.Visit) AS Recent 
     FROM 
      PatientVisit pv 
      INNER JOIN PatientProfile pp ON pv.PatientProfileId = pp.PatientProfileId 
              AND pp.PatientStatusMId = @Active 
     WHERE 
      pp.PatientProfileId IN (SELECT 
             a.OwnerId 
            FROM 
             Appointments a 
             INNER JOIN PatientProfile pp ON a.OwnerId = pp.PatientProfileId 
                     AND a.ApptKind = 1 
                     AND pp.PatientStatusMId = @Active 
            GROUP BY 
             a.OwnerId , 
             a.ApptKind 
            HAVING 
             MAX(a.ApptStart) < '07/30/2005') 
     GROUP BY 
      dbo.FormatName(pp.Prefix , pp.First , pp.Middle , pp.Last , pp.Suffix) , 
      pp.PatientProfileId 
     HAVING 
      MAX(pv.Visit) < '07/30/2005' 

/*Patients without a Appointment*/ 

IF 1 = 1 
    INSERT INTO #Temp 
      SELECT 
       dbo.FormatName(pp.Prefix , pp.First , pp.Middle , pp.Last , pp.Suffix) AS Name , 
       pp.PatientProfileId , 
       NULL AS RecentId , 
       NULL AS Recent 
      FROM 
       PatientProfile pp 
       LEFT JOIN (SELECT * FROM Medlists WHERE TableName = 'PatientProfileStatus') ml1 ON pp.PatientStatusMId = ml1.MedlistsId 
       LEFT JOIN Appointments a ON a.Ownerid = pp.PatientProfileId 
              AND a.ApptKind = 1 
       LEFT JOIN PatientVisit pv ON a.PatientVisitId = pv.PatientVisitId 
      WHERE 
       ml1.Code = 'A' 
       AND a.ownerid IS NULL 
       AND --Filter on Age 
       (
        ((
        '-1' = '-1' 
        AND '40' = '125' 
        ) 
        OR (CAST((DATEDIFF(DAY , pp.Birthdate , GETDATE())/365.25) AS INT) BETWEEN ('-1') AND ('40'))) 
       ) 

/*Patients without a Patient Visit*/ 

IF 0 = 1 
    INSERT INTO #Temp 
      SELECT 
       dbo.FormatName(pp.Prefix , pp.First , pp.Middle , pp.Last , pp.Suffix) AS Name , 
       pp.PatientProfileId , 
       NULL AS RecentId , 
       NULL AS Recent 
      FROM 
       PatientProfile pp 
       LEFT JOIN PatientVisit pv ON pv.PatientProfileid = pp.PatientProfileid 
       LEFT JOIN (SELECT * FROM Medlists WHERE TableName = 'PatientProfileStatus') ml1 ON pp.PatientStatusMId = ml1.MedlistsId 
      WHERE 
       ml1.Code = 'A' 
       AND pv.patientprofileid IS NULL 
       AND --Filter on Age 
       (
        ((
        '-1' = '-1' 
        AND '40' = '125' 
        ) 
        OR (CAST((DATEDIFF(DAY , pp.Birthdate , GETDATE())/365.25) AS INT) BETWEEN ('-1') AND ('40'))) 
       ) 

DECLARE curPatient CURSOR FORWARD_ONLY READ_ONLY LOCAL 
FOR 
    SELECT 
     t.PatientProfileId 
    FROM 
     #Temp t 
     JOIN PatientProfile pp ON t.PatientProfileId = pp.PatientProfileId 
     JOIN PatientVisit pv ON pp.PatientProfileId = pv.PatientProfileId 
           AND pv.PatientVisitId = t.RecentId 
    WHERE 
     --Filter on Age 
     (
      ((
      '-1' = '-1' 
      AND '40' = '125' 
      ) 
      OR (CAST((DATEDIFF(DAY , pp.Birthdate , GETDATE())/365.25) AS INT) BETWEEN ('-1') AND ('40'))) 
     ) 

OPEN curPatient 
FETCH NEXT FROM curPatient INTO @PatientProfileId 

WHILE @@FETCH_STATUS = 0 
    BEGIN 

     UPDATE 
      PatientProfile 
     SET 
      PatientStatusMId = @Inactive , 
      pstatus = 'I' 
     FROM 
      PatientProfile P 
      INNER JOIN #Temp t ON t.PatientProfileID = P.PatientProfileID 
     WHERE 
      p.PatientProfileId = @PatientProfileId 


     FETCH NEXT FROM curPatient INTO @PatientProfileId 
    END 

CLOSE curPatient 
DEALLOCATE curPatient 

DROP TABLE #Temp 
+1

...您的表「MedLists」似乎是多域(給定'tablename'列),可能應該分解。有沒有我不知道的東西,或者'DATEDIFF(年,pp.birthdate,GETDATE())'年返回一個人的年齡(注意你現在的方法除以365.25,並不總是給出正確的結果)。您似乎正在加入大量表格,以獲取最終不會使用的信息 - 爲什麼,以及是否可以刪除引用(例如,不使用'patientName')?以這種方式旋轉游標是**不是**更新表的最有效方法。 –

回答

0

這將限制給患者預約

LEFT JOIN Appointments a 
     ON a.Ownerid = pp.PatientProfileId 
     AND a.ApptKind = 1 

這將限制對患者進行了訪問

LEFT JOIN PatientVisit pv 
     ON pv.PatientProfileid = pp.PatientProfileid 

嘗試

LEFT OUTER JOIN Appointments a 
      ON a.Ownerid = pp.PatientProfileId 
      AND a.ApptKind = 1 
Where a.Ownerid is null 


LEFT OUTER JOIN PatientVisit pv 
     ON pv.PatientProfileid = pp.PatientProfileid 
Where pv.PatientProfileid is null