select u.documentID, u.fileID, u.uri, d.start, s.startDocumentHasNoURI, (select top 1 uri from uris document_uri where document_uri.documentID = u.documentID order by document_uri.dateInsert desc) as documentPath, (select top 1 uri from uris file_uri where file_uri.fileID = u.fileID order by file_uri.dateInsert desc) as filePath from uris u left join documents d on d.documentID = u.documentID left join sites s on s.siteID = d.siteID where ( u.documentID in ( select documentID from documents where (siteID = ? or siteID is null or siteID = (select masterSiteId from sites where siteId = ? )) and active = 1 ) or u.documentID is null ) and Lower(u.uri) = ? order by u.dateInsert desc;