使用Case语句排序,因此使用两列进行排序

时间:2014-03-28 17:53:18

标签: tsql sql-server-2008-r2

我有这个Order By声明:

ORDER BY
CASE WHEN @SortID = 1 AND @SortDirection = 'ASC' THEN ProjectID END ASC,
CASE WHEN @SortID = 1 AND @SortDirection = 'DESC' THEN ProjectID END DESC

我有另一个名为DateSort的列。我需要的是:

当@SortID = 1和@SortDirection =' ASC' ...我想按ProjectID ASC,DateSort ASC

排序

当@SortID = 1和@SortDirection =' DESC' ...我想按ProjectID DESC,DateSort ASC

排序

即。无论ProjectID是按ASC还是DESC排序,我都希望记录也按DateSort ASC排序

1 个答案:

答案 0 :(得分:0)

这不起作用吗?

ORDER BY
CASE WHEN @SortID = 1 AND @SortDirection = 'ASC' THEN ProjectID END ASC,
CASE WHEN @SortID = 1 AND @SortDirection = 'DESC' THEN ProjectID END DESC,
DateSort ASC