将数字转换为逗号分隔的数字并按数字排序

Convert Numbers into Comma Separated Numbers and sort numerically

我得到了过去 3 个月的每小时销售额

USE [SambaPos]
GO
SELECT
    DATEPART(HOUR, [LastUpdateTime]) AS [Hour], 
    FORMAT(SUM([TotalAmount]), 'N0') AS [Sales]
FROM
    Tickets
WHERE
    [LastUpdateTime] > 
    DATEADD(MONTH, 
        DATEDIFF(MONTH, 0, 
            DATEADD(MONTH, -3, GETDATE())
        ), 0
    ) 
    AND
    [LastUpdateTime] < GETDATE()
GROUP BY
    DATEPART(HOUR, [LastUpdateTime])
ORDER BY
    [Sales] DESC

使用 Format 函数格式化字符串中的数字并在字符串列上排序不会给出按数字排序的数字。那么,如何保留数字排序和字符串格式。

这是按字符串排序的销售结果,我希望它们按数字排序,以逗号分隔格式表示。

谢谢

您应该能够在 ORDER BY 子句中使用原始值。

尝试将您的查询更改为以下内容

USE [SambaPos]
GO
SELECT
    DATEPART(HOUR, [LastUpdateTime]) AS [Hour], 
    FORMAT(SUM([TotalAmount]), 'N0') AS [Sales]
FROM
    Tickets
WHERE
    [LastUpdateTime] > 
    DATEADD(MONTH, 
        DATEDIFF(MONTH, 0, 
            DATEADD(MONTH, -3, GETDATE())
        ), 0
    ) 
    AND
    [LastUpdateTime] < GETDATE()
GROUP BY
    DATEPART(HOUR, [LastUpdateTime])
ORDER BY
    SUM([TotalAmount]) DESC