📅  最后修改于: 2023-12-03 14:47:45.308000             🧑  作者: Mango
In SQL Server, the SUM
function is used to calculate the total of a column's numerical values. This function is often used in conjunction with the GROUP BY
clause to aggregate data based on one or more columns.
SELECT column1, SUM(column2)
FROM table
GROUP BY column1;
In the example above, SUM(column2)
calculates the total of column2
for each distinct value in column1
.
The SUM
function can also be used with the OVER
clause to calculate running totals or cumulative sums.
SELECT column1, column2, SUM(column2) OVER (ORDER BY column1)
FROM table;
In the example above, the SUM(column2) OVER (ORDER BY column1)
calculates the cumulative total of column2
for each row, ordered by column1
.
Overall, the SUM
function is a powerful tool in SQL Server that can be used to calculate totals and running totals, and to aggregate data based on one or more columns.