Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. SELECT
  2. PPPS.PatientPhysicianPatientSurveyId,
  3. P.ProcedureId,
  4. PS.SurveyId,
  5. SPD.SurveyPhysicianDurationId AS DurationId,
  6. SPDN.SurveyPhysicianDurationNotificationId AS NotificationId,
  7. SPO.Ordinal,
  8. DATEADD(HOUR, SPDN.Hours_Before_Notification, PPPS.DueDate) AS NotificationDate,
  9. PA.Email AS Recipient,
  10. AG.AnatomyGroupName + ' SURGERY QUESTIONNAIRE' +
  11. CASE
  12. WHEN
  13. SPDN.Hours_Before_Notification <> (
  14. SELECT MIN(SPDN2.Hours_Before_Notification)
  15. FROM Survey_Physician_Duration_Notification SPDN2
  16. WHERE SPDN2.SurveyPhysicianDurationId = SPD.SurveyPhysicianDurationId
  17. )
  18. THEN ' *REMINDER*'
  19. ELSE ''
  20. END AS [Subject],
  21. CASE WHEN PPPS.ShowDate > CURRENT_TIMESTAMP THEN 1 ELSE 0 END AS IsFutureNotification
  22. FROM Patient_Physician_PatientSurvey PPPS
  23. JOIN dbo.Survey_Physician_Duration SPD ON SPD.SurveyPhysicianDurationId = PPPS.SurveyPhysicianDurationId AND SPD.IsActive=1
  24. JOIN dbo.Survey_Physician SP ON SP.SurveyPhysicianId = SPD.SurveyPhysicianId AND SP.IsActive=1 AND SP.FilledByPatient = 1 AND SP.NotificationTemplate IS NOT NULL AND SP.SendSurveyNotification=1
  25. JOIN dbo.PatientSurvey PS ON PS.SurveyId = SP.SurveyId AND PS.IsActive=1
  26. JOIN dbo.Survey_Physician_Duration_Notification SPDN ON SPDN.SurveyPhysicianDurationId = SPD.SurveyPhysicianDurationId
  27. JOIN dbo.Survey_Physician_Ordinal SPO ON SPO.SurveyId = SP.SurveyId AND (SPO.PhysicianId = SP.PhysicianId OR SPO.ClientId = SP.ClientId)
  28. JOIN dbo.[Procedure] P ON P.ProcedureId = PPPS.ProcedureId AND P.IsActive = 1
  29. JOIN dbo.Surgery S ON S.SurgeryId = P.SurgeryId AND S.IsActive = 1 AND S.SurgeryStatusId <> 5
  30. JOIN dbo.AnatomyGroup AG ON AG.AnatomyGroupId = S.AnatomyGroupId AND SPO.AnatomyGroupId = S.AnatomyGroupId
  31. JOIN dbo.Patient PA ON PA.PatientId = P.PatientId AND PA.ReceivesEmail=1 AND PA.Email IS NOT NULL
  32. WHERE
  33. PPPS.DateFinished IS NULL
  34. AND NOT EXISTS(
  35. SELECT TOP 1 1 FROM
  36. OutboundEmail OE
  37. WHERE OE.DurationId = PPPS.SurveyPhysicianDurationId
  38. AND OE.NotificationId = SPDN.SurveyPhysicianDurationNotificationId
  39. AND OE.PatientId = PA.PatientId
  40. )
  41. AND P.PatientId in (254014,254013,254016)
  42. AND DATEADD(HOUR, SPDN.Hours_Before_Notification, PPPS.DueDate) >= GETDATE()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement