我有一个程序可以在一个时间示例(CreateDate BETWEEN'2012/1/21'和'2012/2/19')之间返回每日报告
我想要这个结果
1/21
1/22
1/23
.
2/1
.
.
但是输出会根据日期自动排序!如下:
2/1
2/2
.
2/15
1/21
1/22
1/23
.
.
我无法在(createdate)之前使用order,因为我有以下选择:
SELECT COUNT(UserId) AS c, DAY(CreateDate) AS d
FROM dbo.aspnet_Membership
WHERE (CreateDate BETWEEN '2012/1/21' AND '2012/2/19')
GROUP BY DAY(CreateDate)
最佳答案
如果您想按月和日排序,那么您当然需要一个月。
而且,您始终需要一个ORDER BY来保证结果的顺序。
SELECT COUNT(UserId) AS c, MONTH(CreateDate) AS m, DAY(CreateDate) AS d
FROM dbo.aspnet_Membership
WHERE (CreateDate BETWEEN '2012/1/21' AND '2012/2/19')
GROUP BY MONTH(CreateDate) AS m, DAY(CreateDate) AS d
ORDER BY MONTH(CreateDate) AS m, DAY(CreateDate) AS d
在这种情况下,由于GROUP BY(这意味着在MySQL中为ORDER BY,而在其他RDBMS中则不是),因此得到的顺序是巧合的。
关于sql - 选择自动订购,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9294130/