0

I have a query which returns a result set like this:

Proj  |  release  |  releaseDt
1     |   2       |  1/2/2013
1     |   1       |  4/5/2012
2     |   1       |  [null]
3     |   1       |  22/2/2013
1     |   3       |  [null]
3     |   2       |  [null]

I need to sort this by releaseDt, but I need to have all the records for that Proj together.

After sorting, the result should be something like this:

Proj | release |   releaseDt
1    |  2      |   1/2/2013
1    |  1      |   4/5/2012
1    |  3      |   [null]
3    |  1      |   22/2/2013
3    |  2      |   [null]
2    |  1      |   [null]

How can I do this with SQL Server?

4

3 回答 3

3

您需要做的就是对表格进行两次排序:首先按Proj,然后按releaseDt

SELECT *
FROM mytable
ORDER BY
  Proj ASC,
  releaseDt DESC
于 2013-06-13T15:35:50.130 回答
3

尝试

ORDER BY proj ASC, releaseDt DESC

应该做的伎俩!

于 2013-06-13T15:36:51.347 回答
1

您希望按项目的最早发布日期排序,然后按项目中的发布日期排序。

您可以使用窗口函数获取最早的日期,然后使用它进行排序:

select t.Proj, t.release, t.releaseDt
from (select t.*, min(releasedt) over (partition by proj) as minrdt
      from t
     ) t
order by t.minrdt, t.proj, t.releaseDt
于 2013-06-13T15:58:17.913 回答