select top (10) r.requestId, r.trackingId, r.rank,
case when deployed = 'Y' then 4 when statusCode = 'X' then 2 when statusCode = 'B' then 2 else 3 end statusSort,
coalesce( coalesce( firstCompletedDate, firstActiveDate ), dueDate ) sortDate,
firstActiveDate, firstCompletedDate, dueDate, deployed, description
from tbl_SCRUM_Requests r
inner join tbl_SCRUM_RequestNotes n
on n.requestId = r.requestId
and n.noteType = 'release'
where statusCode in ( 'X', 'B', 'A' )
select * from qmd_requestsWithReleaseNotes
order by
statusSort,
rank,
sortDate desc,
trackingId desc