开发者

select sum in sql server using one query

开发者 https://www.devze.com 2023-03-24 18:40 出处:网络
I have a table in my database wi开发者_如何学编程th two columns as int for example cal1 and cal2.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号