我有一张桌子,看起来像这样:

ComputerID | Free_Space | Disk_Size
------------------------------------------
Computer1  | 10         | 15
Computer1  | 10         | 10
Computer1  | 05         | 10
Computer2  | 05         | 05
Computer2  | 05         | 10
Computer3  | 11         | 15
Computer3  | 04         | 05


我需要实现以下目标:

ComputerID | Free_Space | Disk_Size
------------------------------------------
Computer1  | 25         | 35
Computer2  | 10         | 15
Computer3  | 15         | 20


我已经尝试过类似的方法,但是没有成功。

 SELECT ComputerID,
 SUM(free_space/1000000000) AS 'Total Free Space',
 SUM(disk_size/1000000000) AS 'Total Size'
 FROM myTable
 ORDER BY ComputerID


任何帮助将非常感激。

最佳答案

您需要一个GROUP BY子句

SELECT
    ComputerID,
    SUM(free_space/1000000000) AS "Total Free Space",
    SUM(disk_size/1000000000) AS "Total Size"
FROM
    myTable
GROUP BY
    ComputerID
ORDER BY
    ComputerID


还要注意,大多数SQL方言使用单引号作为字符串分隔符,将双引号用作列名称的分隔符。使用SQL Server,您还可以使用方括号

[Total Free Space]

10-06 13:20