分组依据,按日期排序

时间:2012-07-03 18:55:09

标签: sql group-by sql-order-by

我有近15000个数据行,第一列包含格式的日期: 2012-05-10 09:00:00.000

我需要按年份,按月,然后按天,然后按小时进行排序,例如:

         2012-05-10 09:00:00.000
         2012-05-10 10:00:00.000
         2012-05-10 11:00:00.000
         2012-05-10 12:00:00.000
         2012-05-11 09:00:00.000
         2012-05-11 10:00:00.000
         2012-05-11 11:00:00.000
         2012-05-11 12:00:00.000
         2012-06-01 02:00:00.000
         2012-06-01 03:00:00.000
         2012-06-01 04:00:00.000
         2012-06-01 05:00:00.000

当前SQL执行此操作的查询如下:

SELECT MIN(Datetime) GROUP BY DATEPART(M,jmusa_LOG1.DateTime),DATEPART(D,jmusa_LOG1.DateTime),DATEPART(HH,jmusa_LOG1.DateTime) HAVING MIN(jmusa_LOG1.DateTime) NOT IN(SELECT DateTime FROM AverageRawData)

ORDER BY DATEPART(M,jmusa_LOG1.DateTime),DATEPART(D,jmusa_LOG1.DateTime),DATEPART(HH,jmusa_LOG1.DateTime)

1 个答案:

答案 0 :(得分:1)

您正在描述正常的日期排序,因此您可以这样做:

select MyDate
from AverageRawData
order by MyDate

如果您不想复制,请像这样添加DISTINCT

select distinct MyDate
from AverageRawData
order by MyDate

如果这不符合您的要求,请提供用于生成输出示例的示例数据。