Попробуйте это
ORDER BY
CASE WHEN @sortColumnName = 'LastPayCheckDate' AND @sortDirection = 'ASC'
THEN MAX(PayCheckDate) END ASC,
CASE WHEN @sortColumnName = 'LastPayCheckDate' AND @sortDirection = 'DESC'
THEN MAX(PayCheckDate) END DESC
Пример
create table Test (id int, somevalue int)
insert Test values(1,1)
insert Test values(2,1)
insert Test values(3,2)
insert Test values(3,2)
insert Test values(4,2)
запустите это за 1 выстрел
declare @sortDirection char(4)
select @sortDirection = 'DESC'
select somevalue, COUNT(*)
from Test
group by somevalue
order by case when @sortDirection = 'ASC'
then COUNT(*) end asc,
case when @sortDirection = 'DESC'
then COUNT(*) end desc
select @sortDirection = 'ASC'
select somevalue, COUNT(*)
from Test
group by somevalue
order by case when @sortDirection = 'ASC'
then COUNT(*) end asc,
case when @sortDirection = 'DESC'
then COUNT(*) end desc