select sum in sql server using one query
I have a table in my database wi开发者_如何学编程th two columns as int for example cal1
and cal2
.
I make the sum from each row as in select ( cal1 + cal2) from cat as total
, now I want to do the sum from all columns total if is possible.
You can do the addition in the SUM :
SELECT SUM(cal1+cal2) AS total
FROM cat
SELECT (SUM(cal1) + SUM(cal2)) AS TotalSum FROM cat
Thats to sum the values of all rows together. If you want to sum all columns up, you have to specifically write their names into the column list.
You want 2 totals in one go?
SELECT
cal1+cal2 AS PerRowTotal,
SUM(cal1+cal2) OVER () AS AllRowTotal
FROM
cat
精彩评论