with submissions as ( select s.submissionId, s.submissionTime, s.emailId, m.fspro_userid, m.lastName, m.firstName, m.email, m.ownerId userOwnerId, m.fsl_tablecode userOwnerType, coalesce( s.submissionStatus, 'APPROVED' ) as submissionStatus, s.workflowRequestId, wrm.firstName toFirstName, wrm.lastName toLastName, s.formId from tbl_WRK_FormSubmissions s with (nolock) left outer join tbl_Fspro_members m with (nolock) on m.fspro_userid = submitUserId left outer join tbl_WRK_userRequests wr with (nolock) on wr.ownerId= s.ownerId and wr.ownerType = s.ownerType and wr.requestId= s.workflowRequestId left outer join tbl_Fspro_members wrm with (nolock) on wrm.fspro_userid = wr.toUserId where s.ownerId = and s.ownerType = ) #forJSON( 'submissions', attributes.datastore, 'submissionTime' )#