在SQL查询中按年份进行二级排序 [英] Doing a secondary sort by year in a SQL query

查看:211
本文介绍了在SQL查询中按年份进行二级排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个表,其中包含ID值和日期,看起来像这样:

So I have a table that with ID values and dates that looks something like this:

ID           Date
0001         1/1/2012
0002         1/2/2010
0002         1/2/2011
0001         1/1/2011
0001         1/1/2010
0002         1/2/2012

基本上,ID值仅对那一年是唯一的-它们会重置下一年.

Basically, the ID values are unique to that year only - they reset the subsequent year.

我希望能够按ID值和日期排序,但是我想进行排序,以便按年份对值进行排序.只是带有辅助日期排序的常规ID会产生以下结果:

I want to be able to sort by ID values and by dates, but I want to do the sorting so that the values are ordered by year. Just a regular sort of ID with a secondary date sort yields this:

ID           Date
0001         1/1/2010
0001         1/1/2011
0001         1/1/2012
0002         1/2/2010
0002         1/2/2011
0002         1/2/2012

但是我想要一个查询,该查询生成一个看起来像这样的表:

But I would like a query that generates a table that looks like this:

ID           Date
0001         1/1/2010
0002         1/2/2010
0001         1/1/2011
0002         1/2/2011
0001         1/1/2012
0002         1/2/2012

这可能吗?

推荐答案

这是怎么回事:

order by year(date), id

这篇关于在SQL查询中按年份进行二级排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆