with batches as ( select ownerId, ownerType, batchTime, startTime, processedTime, convert( varchar, batchId ) as batchId, parentBatchId, fileFormat, 100.0 progress, datediff( minute, startTime, processedTime ) minutes from tbl_EAI_inboundBatches with (nolock) where batchTime >= '#_startDay#' and batchTime < '#_endDay#' and ownerId > 0 and fileFormat not in ( '$ProductFocusFlag', '$prdDataIntegrityScan', '$TPM_ARCHIVECLAIMS', '$TestStudioPerformanceLogProcessor' ) and ( fileFormat in ( '$RecalculateBusinessPlans', '$OperatorAnalyzerPostProcessing', '$1fsMembershipStatusUpdate' ) or fileFormat like '$%ETL' or fileFormat like '$LOAD%' or fileFormat like '$update1fsMembershipStatus%' or ( ownerId in ( 1, 54 ) and ownerType = 'MFR ')) and processedTime is not null ), clients as ( select distinct ownerId, ownerType, ownerType + convert( varchar, ownerId ) batchId, null batchTime, null startTime, null processedTime, null parentBatchId, null fileFormat, null progress, null minutes from batches ) select ownerId, ownerType, batchTime, startTime, processedTime, batchId, parentBatchId, fileFormat, progress, minutes from batches union all select ownerId, ownerType, batchTime, startTime, processedTime, batchId, parentBatchId, fileFormat, progress, minutes from clients order by batchTime for json auto, include_null_values