按两列排序和排序

编程入门 行业动态 更新时间:2024-10-28 12:24:28
本文介绍了按两列排序和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个查询,它返回这样的结果集:

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]

我需要按 releaseDt 对其进行排序,但我需要将那个 Proj 的所有记录放在一起.

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]

如何使用 SQL Server 执行此操作?

How can I do this with SQL Server?

推荐答案

您希望先按项目的最早发布日期排序,然后再按项目内的发布日期排序.

You want to sort by the earliest release date for a project and then by the release date within a project.

您可以使用窗口函数获取最早的日期,然后将其用于排序:

You can get the earliest date using a window function, and then use that for sorting:

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

更多推荐

按两列排序和排序

本文发布于:2023-10-28 02:33:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535252.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按两列

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!