Sure, here's the query:-
WITH myMessages AS (
SELECT msg.id, msg.m_username, msg.c_name, msg.m_Created, msg.m_subject, msg.c_msg, msg.m_read, msg.m_dateRead, msg.m_replied, msg.m_dateReplied, msg.m_forward, msg.importance
,ROW_NUMBER() OVER (ORDER BY msg.m_read asc,
case
when @order=1 then...