OrderBy子句排序问题?

时间:2014-07-18 05:58:32

标签: sql-server

我试图在SQL Server中按Date排序我面临一个奇怪的问题。我应该解释2个案例,以帮助您理解我的问题。

案例1:

select [MonthName] 
from prod.[dim date] 
where F_Year = 2014   
order by [Date]

我的输出:

june june june july july july july august  
// Here I get duplicates but order by is working as expected 

案例2:尝试使用" distinct" 删除重复项

select distinct [MonthName] 
from prod.[dim date] 
where F_Year = 2014   
order by [Date]

我的输出:

August july june 
// Order By not working as expected (ordering alphabetical wise ) . 

感谢任何解决方法

1 个答案:

答案 0 :(得分:0)

首先,您的查询似乎正在处理一个表,其中3个字段表示可能是单个日期,或者可能是2个 - 没有结构很难分辨,但我可以看到:F_Year,[Date]& [MONTHNAME]。因此,如果这些实际上代表一个日期,则恢复到该单个日期并使用格式来确定F_Year和MonthName。

其次,请查看这些帖子,以获得对您问题的可能答案:

Sort by Date in SQL

Convert Month Number to Month Name Function in SQL