Thanks, now I am changing it to Temptable and I am also sending you the SP, if you can give valuable input on that, so it will be great for me.
And problem area, I already send above
CREATE Procedure [dbo].[SP_DQF_ALL_RunReport]
@RundateStart Date,
@RunDateEnd Date
As
Declare @TempTable Table (RunDate Date, CompanyID Varchar(100) null, FaxID DQF_TYPE_FAXID, DriverID varchar(200) NULL, StatusMessage Varchar(max), ProcessingStatus varchar(40), RecordType Varchar(2))
Declare @TempTableDocument Table (RunDate Date, CompanyID Varchar(100) null, FaxID DQF_TYPE_FAXID, DocumentType Varchar(100), DriverID varchar(200) NULL, StatusMessage Varchar(max), ProcessingStatus varchar(40), RecordType Varchar(2))
Insert Into @TempTable (RunDate, CompanyID, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Cast(CPS.DateofCreation as Date), CDT.CompanyName, CPS.FaxID, null, 'Pending for Data Entry', 'Pending Data Entry','FX'
from CMS_CPScreenData_Temp CPS
Inner Join DQF_FAXID_AUDIT_HDR HDR ON HDR.FaxID = CPS.FaxID And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd and CPS.FaxID > 0
Inner Join CustomerDetails CDT ON CDT.FADV_CustomerID = HDR.CustomerID
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'Exception - Exceptional Document', 'Partial Document','FX'
from CMS_CPScreenData CPS
Inner Join FaxId_Document_Relation FDR on CPS.FaxID = FDR.FaxID and Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd And CPS.FaxID > 0
and CPS.IsReadyForRoboTwoProcessing = 2 and FDR.Robo2Remarks = 'Exceptional Document'
Where CPS.FaxID not in (select faxid from @TempTable)
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'Exception - Exceptional Document', 'Partial Document','FX'
from CMS_CPScreenData CPS
Inner Join FaxId_Document_Relation FDR ON CPS.FaxID = FDR.FaxID And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd And CPS.FaxID > 0
Inner Join DocumentType DOT ON DOT.CustomerID = CPS.CustomerID and FDR.DocumentID = DOT.DocumentTypeID and DOT.IsException = 1
Where CPS.FaxID not in (select faxid from @TempTable)
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'Exception - Unable to Login to Right Choice' , 'Exception','FX'
from CMS_CPScreenData CPS
Inner Join ErrorLog ELG ON CPS.CPScreenDataID = ELG.CPScreenDataID and ELG.ErrorMessage like '%Right Choice Login%'
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd and CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Group By Cast(CPS.DateofCreation as Date), CPS.FaxID, ELG.CPSCreenDataID
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'No Driver Found for SSN', 'Exception','FX'
from CMS_CPScreenData CPS
Inner Join ErrorLog ELG ON CPS.CPScreenDataID = ELG.CPScreenDataID and ELG.ErrorMessage like '%Process CMS Faxes%'
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd and CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Group By Cast(CPS.DateofCreation as Date), CPS.FaxID, ELG.CPSCreenDataID
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'System Exception in ROBO 1 - Need to Check', 'Error','FX'
from CMS_CPScreenData CPS
Inner Join ErrorLog ELG ON CPS.CPScreenDataID = ELG.CPScreenDataID and ELG.ErrorMessage like '%System.Exception%' and ELG.RobotID = 1
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd and CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Group By Cast(CPS.DateofCreation as Date), CPS.FaxID, ELG.CPSCreenDataID
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'System Exception in ROBO 2 - Need to Check', 'Error','FX'
from CMS_CPScreenData CPS
Inner Join ErrorLog ELG ON CPS.CPScreenDataID = ELG.CPScreenDataID and ELG.ErrorMessage like '%System.Exception%' and ELG.RobotID = 2
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd and CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Group By Cast(CPS.DateofCreation as Date), CPS.FaxID, ELG.CPSCreenDataID
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'System Exception in ROBO 1 - Need to Check', 'Error','FX'
from CMS_CPScreenData CPS
Inner Join ErrorLog ELG ON CPS.CPScreenDataID = ELG.CPScreenDataID and (ELG.ErrorMessage like '%UIPath.Core%' OR ELG.ErrorMessage like '%UiPath.Core%') and ELG.RobotID = 1
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd and CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Group By Cast(CPS.DateofCreation as Date), CPS.FaxID, ELG.CPSCreenDataID
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'System Exception in ROBO 2 - Need to Check' , 'Error','FX'
from CMS_CPScreenData CPS
Inner Join ErrorLog ELG ON CPS.CPScreenDataID = ELG.CPScreenDataID and (ELG.ErrorMessage like '%UIPath.Core%' OR ELG.ErrorMessage like '%UiPath.Core%') and ELG.RobotID = 2
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd and CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Group By Cast(CPS.DateofCreation as Date), CPS.FaxID, ELG.CPSCreenDataID
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'Exception - ' + Replace(Substring(Stuff((Select ', ' + EL1.ErrorMessage From ErrorLog EL1 Where EL1.CPScreenDataID = ELG.CPSCreenDataID FOr XML PATH ('')),1,1,''), 1, 50),', ,',','), 'Exception'
,'FX'
from CMS_CPScreenData CPS
Inner Join ErrorLog ELG ON CPS.CPScreenDataID = ELG.CPScreenDataID And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd and CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Group By Cast(CPS.DateofCreation as Date), CPS.FaxID, ELG.CPSCreenDataID
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Cast(CPS.DateofCreation as Date), Cast(Substring(Cast(CPS.FaxID * -1 as Varchar(20)),1,9) as int), NULL, 'Data Entry Not Complete for Dummy FaxID' , 'Pending Duplicate Entry','FX'
from CMS_CPScreenData_Temp CPS
Where Cast(Substring(Cast(CPS.FaxID * -1 as Varchar(20)),1,9) as int) not in (select faxid from @TempTable)
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd And CPS.FaxID < 0 AND cps.NoOfDataEntries < 2
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Cast(CPS.DateofCreation as Date), Cast(Substring(Cast(CPS.FaxID * -1 as Varchar(20)),1,9) as int), null, 'Data Entry Complete for Dummy FaxID but FaxID not released from hold', 'Hold','FX'
from CMS_CPScreenData_Temp CPS
Inner Join RoboActivity RAC On CPS.RoboActivitiesID = RAC.RoboActivitiesID And Cast(RAC.StartTime as Date) >= @RundateStart and Cast(RAC.StartTime as Date) <= @RundateEnd and CPS.FaxID < 0 AND cps.NoOfDataEntries = 2
Where Cast(Substring(Cast(CPS.FaxID * -1 as Varchar(20)),1,9) as int) not in (select faxid from @TempTable)
And CPS.FaxID < 0 AND cps.NoOfDataEntries < 2 And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'Data Entry Complete - Pending for ROBO 1 Processing', 'Pending ROBO1','FX'
from CMS_CPScreenData CPS
Inner Join FaxId_Document_Relation FDR ON CPS.FaxID = FDR.FaxID And FDR.ProcessingState = 0
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd And CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'Processing Complete', 'Success','FX'
from CMS_CPScreenData CPS
Where CPS.FaxID not in (select faxid from @TempTable) and CPS.IsReadyForRoboTwoProcessing = 2
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd And CPS.FaxID > 0
Insert Into @TempTable (RunDate, CompanyID, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Cast(CPS.DateofCreation as Date), CDT.CompanyName, CPS.FaxID, null, 'Pending for ROBO 2 Processing', 'Pending Robo2','FX'
from CMS_CPScreenData CPS
Inner Join CustomerDetails CDT ON CDT.CompanyID = CPS.CustomerID
Where CPS.FaxID not in (select faxid from @TempTable) and CPS.IsReadyForRoboTwoProcessing = 1
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd And CPS.FaxID > 0
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'Exception - Processed By Agent', 'Processed By Agent','FX'
from CMS_CPScreenData CPS
Inner Join FaxId_Document_Relation FDR ON CPS.FaxID = FDR.FaxID And FDR.DriverFileCreationRemark = 'No Record found'
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd And CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'Exception - FaxID Locked', 'Exception','FX'
from CMS_CPScreenData CPS
Inner Join FaxId_Document_Relation FDR ON CPS.FaxID = FDR.FaxID And FDR.DriverFileCreationRemark like '%Fax ID Record is Locked%'
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd And CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'Exception - Multiple Driver file exists' , 'Exception','FX'
from CMS_CPScreenData CPS
Inner Join FaxId_Document_Relation FDR ON CPS.FaxID = FDR.FaxID And FDR.DriverFileCreationRemark like '%Multiple Driver file%'
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd And CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'Exception - Driver File is Locked' , 'Exception','FX'
from CMS_CPScreenData CPS
Inner Join FaxId_Document_Relation FDR ON CPS.FaxID = FDR.FaxID And FDR.DriverFileCreationRemark like '%Driver File is Locked%'
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd And CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Insert Into @TempTable (RunDate, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Distinct Cast(CPS.DateofCreation as Date), CPS.FaxID, null, 'Exception - Sufficient Information Not Available to Proceed' , 'Exception','FX'
from CMS_CPScreenData CPS
Inner Join FaxId_Document_Relation FDR ON CPS.FaxID = FDR.FaxID And FDR.DriverFileCreationRemark like '%Relevent information%'
And Cast(CPS.DateofCreation as Date) >= @RundateStart And Cast(CPS.DateofCreation as Date) <= @RundateEnd And CPS.FaxID > 0
Where CPS.FaxID not in (select faxid from @TempTable)
Insert Into @TempTable (RunDate, CompanyID, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Cast(RAC.StartTime as Date), CDT.CompanyName, FaxID, null, 'Deleted - No Data Entry', 'Deleted - Pending Data Entry','FX'
from DQF_FAXID_AUDIT_HDR AUH
Inner Join RoboActivity RAC On AUH.RoboActivityID = RAC.RoboActivitiesID And Cast(RAC.StartTime as Date) >= @RundateStart and Cast(RAC.StartTime as Date) <= @RundateEnd and AUH.ProcessingState = 1
Inner Join CustomerDetails CDT On AUH.CustomerID = CDT.FADV_CustomerID
Where AUH.FaxID not in (select faxid from @TempTable)
And AUH.FaxID not in (Select faxid from CMS_CPScreenData)
Insert Into @TempTable (RunDate, CompanyID, FaxID, DriverID, StatusMessage, ProcessingStatus, RecordType)
Select Cast(RAC.StartTime as Date), CDT.CompanyName, FaxID, null, 'Unknown Error', 'Error','FX'
from DQF_FAXID_AUDIT_HDR AUH
Inner Join RoboActivity RAC On AUH.RoboActivityID = RAC.RoboActivitiesID And Cast(RAC.StartTime as Date) >= @RundateStart and Cast(RAC.StartTime as Date) <= @RundateEnd and AUH.ProcessingState = 1
Inner Join CustomerDetails CDT On AUH.CustomerID = CDT.FADV_CustomerID
Where AUH.FaxID not in (select faxid from @TempTable)
Update @TempTable
Set StatusMessage = 'Processed By Agent',
ProcessingStatus = 'Processed By Agent'
Where StatusMessage like '%Processed By Agent%'
Update @TempTable
Set ProcessingStatus = 'Partial Document'
Where StatusMessage like '%Partial%'
Update TMP
Set DriverID = FDR.DriverID,
CompanyID = CDT.CompanyName
from @TempTable TMP
Inner Join (Select FaxID, CompanyID, LTrim(Replace(Stuff((Select ', ' + ISNULL(Cast(FD1.DriverID as varchar(10)),'') From FaxId_Document_Relation FD1 Where FD1.FaxID = FD2.FaxID For XML PATH ('')),1,1,''),' ,','')) as DriverID
From FaxId_Document_Relation FD2
Where FaxID in (select FaxID from @TempTable)
Group By FaxID, CompanyID) as FDR ON TMP.FaxID = FDR.FaxID
Inner Join CustomerDetails CDT on CDT.CompanyID = FDR.CompanyID
Insert Into @TempTableDocument
Select TMP.RunDate, TMP.CompanyID, TMP.FaxID,DOC.DocumentTypeName, TMP.DriverID, ISNULL(FDR.Robo2Remarks, FDR.DriverFileCreationRemark), TMP.ProcessingStatus, 'DL'
From @TempTable TMP
Inner Join FaxId_Document_Relation FDR ON TMP.FaxID = FDR.FaxID
Inner Join DocumentType DOC ON FDR.DocumentID = DOC.DocumentTypeID
Where TMP.StatusMessage not in ('Pending for Data Entry', 'Data Entry Not Complete for Dummy FaxID', 'Data Entry Complete - Pending for ROBO 1 Processing', 'Pending for ROBO 2 Processing' )
Insert Into @TempTableDocument
Select RunDate, CompanyID, FaxID,'Null', DriverID, StatusMessage, ProcessingStatus, RecordType
From @TempTable
select *
from @TempTableDocument
Order By RecordType,RunDate, CompanyID, FaxID
Select RunDate, CompanyID, ProcessingStatus, COUNT(*)
From @TempTableDocument
Where RecordType = 'FX'
Group By RunDate, CompanyID, ProcessingStatus
Thanks