Monday, 5 February 2018

Procedure example

ALTER PROCEDURE [dbo].[SSP_EMRProductivityDocumentsListReport] 

@FromDate DATETIME 
,@ToDate  DATETIME 
,@PracticeID INT 
,@UploadedBy UNIQUEIDENTIFIER 
,@RecordCount INT   
,@PageSize INT   

AS 
--exec [SSP_EMRProductivityDocumentsListReport] '11/5/2013','11/5/2013',1,'2CF8C65C-71CA-46ED-9AE3-E3B524D17E1E',0,50 
/***************************************************************************************************           

BEGIN 
SET NOCOUNT ON; 
BEGIN TRY         
------------------------------GET file seq and description for single uploaded file---------------------------------------------------------------------------                     
SELECT DISTINCT                 
RF.File_Seq AS ID             
,R.IHEResult_Patient_ID_nbr AS PatientID 
,RF.DocumentType AS DocumentTypeID 
,TDT.OfficeConsultationType COLLATE DATABASE_DEFAULT AS DocumentTypeName 
,RF.UploadedOn AS UploadedOn 
,RF.UploadedBy 
,RF.IsMissing
,ISNULL(RF.IsDeleted,0) AS IsDeleted
,RF.DeletedBy 
,ISNULL(CONVERT(VARCHAR,RF.DeletedOn,101),'') AS DeletedOn       
INTO #tempResultFile 
FROM tbl_healthcare_system_IHEResultFiles RF WITH(NOLOCK)                               
JOIN ( 
SELECT             
RANK() OVER (PARTITION BY IHEResultFiles_savedname_txt ORDER BY File_Seq DESC ) seq             
,File_Seq             
FROM dbo.tbl_healthcare_system_IHEResultFiles RF1 WITH(NOLOCK)           
INNER JOIN  dbo.tbl_healthcare_system_IHEResult RS WITH(NOLOCK) ON RS.IHEResult_seq = RF1.IHEResult_seq                   
WHERE           
(ISNULL(RS.IHEResult_Status_txt,'')='') AND (RS.PrID=@PracticeID OR @PracticeID = 0) 
AND RF1.IsPatientUnassigned IS NULL 
AND ISNULL(RF1.IsDeleted,0) = 0 
)TF       
ON TF.File_Seq = RF.File_Seq 
JOIN tbl_healthcare_system_IHEResult R WITH(NOLOCK) ON R.IHEResult_seq = RF.IHEResult_seq                             
JOIN tblOfficeConsultationType TDT WITH(NOLOCK) ON TDT.ID = RF.DocumentType       
WHERE 
TF.Seq=1 
AND RF.IsPatientUnassigned IS NULL 
AND (ISNULL(RF.IsDeleted,0) = 0)
AND (R.PrID=@PracticeID OR @PracticeID = 0) 
AND ((RF.DocumentType  >=3 AND RF.DocumentType <= 7)) 
AND CONVERT(DATE,RF.UploadedOn,101) BETWEEN CONVERT(DATE,@FromDate,101) AND CONVERT(DATE,@ToDate,101) 
AND (RF.UploadedBy = @UploadedBy) 

--------------------------------------------------------------------------------------------------------------                     

CREATE INDEX IX_TRF ON #tempResultFile(ID,PatientID)                 
UPDATE STATISTICS #tempResultFile                 

------------------------------------------------------------------------------------------                 
CREATE TABLE #tempDoc(ID INT,PatientID INT,DocumentTypeID INT,DocumentTypeName VARCHAR(100),UploadedOn DATETIME,UploadedBy UNIQUEIDENTIFIER,IsMissing BIT,IsDeleted INT,DeletedBy UNIQUEIDENTIFIER,DeletedOn VARCHAR(10)) 

------------------------------                 
INSERT INTO #tempDoc                 
SELECT DISTINCT                 
PD.ID AS ID             
,PD.PatientID 
,PD.PatientDocumentsTypeID AS DocumentTypeID   
,TDT.DocumentsType  COLLATE DATABASE_DEFAULT AS DocumentTypeName 
,PD.UploadedOn AS UploadedOn     
,PD.UploadedBy 
,PD.IsMissing 
,ISNULL(PD.IsDeleted,0)
,PD.DeletedBy 
,ISNULL(CONVERT(VARCHAR,PD.DeltedOn,101),'')
FROM tblPatientDocuments PD WITH(NOLOCK)     
JOIN tblDocumentsType TDT WITH(NOLOCK) ON TDT.ID = PD.PatientDocumentsTypeID 
WHERE   
PD.IsPatientUnassigned IS NULL 
AND ((PD.PatientDocumentsTypeID >= 10 AND PD.PatientDocumentsTypeID  < 100)) 
AND CONVERT(DATE,PD.UploadedOn,101) BETWEEN CONVERT(DATE,@FromDate,101) AND CONVERT(DATE,@ToDate,101) 
AND (PD.UploadedBy = @UploadedBy) 
AND (ISNULL(PD.IsDeleted,0) = 0)

--------------------------------------------------------------------------------------------------------------                 

UNION 

SELECT DISTINCT                 
PD.ID AS ID             
,PD.PatientID 
,PD.DocumentTypeID AS DocumentTypeID 
,TDT.DocumentType  COLLATE DATABASE_DEFAULT AS DocumentTypeName 
,PD.UploadedOn AS UploadedOn   
,PD.UploadedBy 
,PD.IsMissing
,ISNULL(PD.IsDeleted,0)
,PD.DeletedBy 
,ISNULL(CONVERT(VARCHAR,PD.DeltedOn,101),'')
FROM tblPatientOtherDocuments PD WITH(NOLOCK)                               
JOIN tblPatientDocumentTypes TDT WITH(NOLOCK) ON (TDT.ID = PD.DocumentTypeID)   
WHERE 
PD.IsPatientUnassigned IS NULL 
AND PD.DocumentTypeID>=101 
AND CONVERT(DATE,PD.UploadedOn,101) BETWEEN CONVERT(DATE,@FromDate,101) AND CONVERT(DATE,@ToDate,101) 
AND (PD.UploadedBy = @UploadedBy) 
AND (ISNULL(PD.IsDeleted,0) = 0)

-------------------------------------------------------------------------------------------------------------                   

UNION   

SELECT DISTINCT                 
OCD.ID AS ID             
,OCD.PatientID 
,OCD.ConsultationTypeID AS DocumentTypeID   
,TDT.OfficeConsultationType  COLLATE DATABASE_DEFAULT AS DocumentTypeName 
,OCD.UploadedOn AS UploadedOn 
,OCD.UploadedBy   
,OCD.IsMissing
,ISNULL(OCD.IsDeleted,0)
,OCD.DeletedBy 
,ISNULL(CONVERT(VARCHAR,OCD.DeletedOn,101),'')
FROM tblOfficeConsulationDocuments OCD WITH(NOLOCK)                           
JOIN tblOfficeConsultationType TDT WITH(NOLOCK) ON TDT.ID = OCD.ConsultationTypeID     
LEFT JOIN tbl_healthcare_system_IHEDescription D WITH(NOLOCK) ON D.IHEDescription_seq = OCD.SpecialtyID     
WHERE  OCD.ConsultationTypeID  IN(1,2,8)                         
AND ISNULL(OCD.IsReferral,0)=0 
AND OCD.IsPatientUnassigned IS NULL 
AND CONVERT(DATE,OCD.UploadedOn,101) BETWEEN CONVERT(DATE,@FromDate,101) AND CONVERT(DATE,@ToDate,101) 
AND (OCD.UploadedBy = @UploadedBy) 
AND (ISNULL(OCD.IsDeleted,0) = 0)
---------------------------------------------------------------------------------------------------                 

UNION 

SELECT ID             
,PatientID 
,DocumentTypeID 
,DocumentTypeName 
,UploadedOn 
,UploadedBy 
,IsMissing
,IsDeleted
,DeletedBy
,DeletedOn
FROM #tempResultFile         
----------------------------------------------------------------------------------------------------- 
;WITH CTE_List AS   

SELECT --DISTINCT 

ROW_NUMBER() OVER(ORDER BY PatientName) AS Row 
,ListSearch.ID AS DocumentID 
,DocumentTypeID 
,DocumentTypeName 
,UploadedOn   
,PatientName 
,IsMissing 
,PatientID 
,PatientDOB
,IsDeleted
,DeletedBy
,DeletedOn
FROM ( 
SELECT --DISTINCT 
tD.ID 
,tD.DocumentTypeID 
,tD.DocumentTypeName 
,tD.UploadedOn 
,tP.ID AS PatientID 
--,CAST(tP.LastName + ' ' + tP.FirstName AS NVARCHAR(120)) AS PatientName
,ISNULL(LTRIM(RTRIM(tP.LastName)),'')+', '+ISNULL(LTRIM(RTRIM(tP.FirstName)),'') as PatientName 
--,tPP.PracticeID 
,tD.IsMissing 
,tD.IsDeleted
,ISNULL(LTRIM(RTRIM(AU1.LastName)),'')+', '+ISNULL(LTRIM(RTRIM(AU1.FirstName)),'') as DeletedBy 
,tD.DeletedOn
,CONVERT(VARCHAR,tP.DOB,101) AS PatientDOB 
FROM  #tempDoc tD   
LEFT JOIN aspnet_Users AU ON tD.UploadedBy = AU.UserId 
LEFT JOIN aspnet_Users AU1 ON tD.DeletedBy = AU1.UserId 
LEFT JOIN tblPatient tP ON tP.ID = tD.PatientID 
WHERE EXISTS(SELECT tPP.UserID FROM  dbo.tblUserToPractice tPP WHERE tPP.UserID = tD.UploadedBy AND (tPP.PracticeID = @PracticeID OR @PracticeID = 0))     
AND (tP.PrID =@PracticeID OR @PracticeID = 0)
AND (ISNULL(tp.PracticeStatusID,0)<7 OR ISNULL(tp.DuplicateStatus,0)=0) 
GROUP BY tD.ID,DocumentTypeID,DocumentTypeName,UploadedOn,tP.ID,ISNULL(LTRIM(RTRIM(tP.LastName)),'')+', '+ISNULL(LTRIM(RTRIM(tP.FirstName)),'')--,tPP.PracticeID, 
,tD.IsMissing,tp.DOB,tD.IsDeleted,ISNULL(LTRIM(RTRIM(AU1.LastName)),'')+', '+ISNULL(LTRIM(RTRIM(AU1.FirstName)),''),tD.DeletedOn
)ListSearch 

SELECT CD.*, (Select COUNT(*) from CTE_List) RecordCount 
FROM CTE_List CD 
WHERE CD.Row Between @RecordCount + 1 AND @RecordCount + @PageSize 
ORDER BY PatientName 

---------------------------------------------------------------------------------------------------------- 

DROP TABLE #tempDoc 
DROP TABLE #tempResultFile 

------------------------------------------------------------------------------------------------------------ 
----------------QUERY STRUCTURE---------------------------------------------------------------------------- 
------------------------------------------------------------------------------------------------------------ 

-- ;WITH CTE_List AS   
--  ( 
--  SELECT DISTINCT ROW_NUMBER() OVER(ORDER BY PrID,PatientName) AS Row 
--  ,ListSearch.ID AS DocumentID 
--  ,DocumentTypeID 
--  ,DocumentTypeName 
--  ,UploadedOn 
--  ,PatientName 
--  ,IsMissing 
--  ,PatientID 
--  ,PatientDOB
--  ,IsDeleted
-- ,DeletedBy
-- ,DeletedOn 
--FROM ( 
-- SELECT DISTINCT                 
-- OCD.ID AS ID 
-- ,OCD.ConsultationTypeID AS DocumentTypeID 
-- ,TDT.OfficeConsultationType AS DocumentTypeName 
-- ,OCD.UploadedOn UploadedOn 
-- ,OCD.PatientID           
-- ,CAST(tP.LastName + ' ' + tP.FirstName AS NVARCHAR(120)) AS PatientName 
-- ,PrID 
-- ,OCD.IsMissing
-- ,ISNULL(OCD.IsDeleted,0) AS IsDeleted
-- ,ISNULL(AU1.LastName,'')+' '+ISNULL(AU1.FirstName,'') as DeletedBy 
-- ,ISNULL(CONVERT(VARCHAR,OCD.DeletedOn,101),'') AS 'DeletedOn'
-- ,CONVERT(VARCHAR,tP.DOB,101) AS PatientDOB 
-- FROM tblOfficeConsulationDocuments OCD WITH(NOLOCK)                           
-- JOIN tblOfficeConsultationType TDT WITH(NOLOCK) ON TDT.ID = OCD.ConsultationTypeID   
-- LEFT JOIN aspnet_Users AU ON OCD.UploadedBy = AU.UserId 
-- LEFT JOIN aspnet_Users AU1 ON OCD.DeletedBy = AU1.UserId 
-- LEFT JOIN tblPatient tP ON tP.ID = OCD.PatientID 
-- WHERE (tP.PrID = @PracticeID OR @PracticeID = 0)     
-- )ListSearch 
-- ) 
-- SELECT CD.*, (Select COUNT(*) from CTE_List) RecordCount 
--  FROM CTE_List CD 
-- WHERE CD.Row Between @RecordCount + 1 AND @RecordCount + @PageSize 
-- ORDER BY PatientName 


END TRY                             
BEGIN CATCH                             
DECLARE @Error VARCHAR(8000) 
SET @Error= CONVERT(VARCHAR,ERROR_NUMBER()) + '*****' + CONVERT(VARCHAR(4000),ERROR_MESSAGE())                             
+ '*****' + ISNULL(CONVERT(VARCHAR,ERROR_PROCEDURE()),'[SSP_EMRProductivityDocumentsListReport] ')                             
+ '*****' + CONVERT(VARCHAR,ERROR_LINE()) + '*****ERROR_SEVERITY='           
+ CONVERT(VARCHAR,ERROR_SEVERITY())                             
+ '*****ERROR_STATE=' + CONVERT(VARCHAR,ERROR_STATE())                             
RAISERROR                             
(                             
@Error, -- Message text.                             
16, -- Severity.                             
1 -- State.                             
)                             
END CATCH                               
END  

No comments:

Post a Comment