订单只按天而不是月

时间:2012-12-02 07:59:47

标签: sql sql-server

嘿,我似乎无法解决这个问题,我有这个问题:

SELECT  DISTINCT CONVERT(VARCHAR(8), time, 4) AS Batch  
FROM Calls 
ORDER BY Batch

按天计算列数而不是常规日期,这意味着我得到的结果如下:

1.11.12
1.12.12
2.11.12
3.11.12
4.11.12

我怎么能解决这个问题? 如果我按时间尝试按顺序排序,查询将不会区分值并扰乱我的所有查询。

这意味着:

SELECT  DISTINCT CONVERT(VARCHAR(8), time, 4) AS Batch , time 
FROM Calls 
ORDER BY time, Batch

将查询排序正常但不会区分值。

1 个答案:

答案 0 :(得分:1)

试试这个

SELECT  DISTINCT CONVERT(VARCHAR(8), time, 4) AS Batch
FROM Calls 
ORDER BY time, Batch

<强>更新

select convert(varchar(8), time, 4) as [Batch]
from [Calls]
group by convert(varchar(8), time, 4)
order by convert(datetime, convert(varchar(8), time, 4), 4)

SQL FIDDLE EXAMPLE