0

我正在尝试使用以下按小时计算订单数量的函数:

SELECT
    COUNT(id) total,
    DATENAME(HOUR, purchased_on) label,
    site
FROM dhs.dbo.orders
WHERE purchased_on >= '01/16/2013'
GROUP BY DATENAME(HOUR, purchased_on), site
ORDER BY label DESC

即使我正在使用 ORDER BY 子句,我也得到以下结果,该子句应该将它们从最高标签到最低标签排列:

total   label   site
2         9     AMAZON
1         9     DHS
2         8     AMAZON
1         7     AMAZON
1         6     AMAZON
1         4     AMAZON
1         12    AMAZON
3         11    AMAZON
1         10    AMAZON
1         10    DHS  

为什么订单不工作?

4

1 回答 1

3

DATENAME返回一个VARCHAR

将其替换为DATEPART

SELECT  COUNT(id) total,
        DATEPART(HOUR, purchased_on) label,
        site
FROM    dhs.dbo.orders
WHERE   purchased_on >= '01/16/2013'
GROUP BY
        DATEPART(HOUR, purchased_on), site
ORDER BY
        label DESC
于 2013-01-17T17:35:51.797 回答