Questions tagged [sum]

The result of adding two or more quantities together.

The sum or total refers to the result of a summation operation. To refer to the operation yields a sum, use the [tag: addition] tag. A summation involves adding two or more numbers or other quantities. It is primarily an arithmetic operation which means it deals with numbers and also other mathematical objects. However, sum can be used in other contexts which may not necessarily be a result of a mathematical operation. Almost all programming languages support addition of numbers using the + operator. Most programming languages support the summation operation through a built-in language construct or as a utility function part of the standard library.


Examples of sum in various programming languages

C++

#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<int> array{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    int sum = std::accumulate(array.begin(), array.end(), 0, std::plus<int>());
    std::cout << sum;
    return 0;
}

Python:

sum_of_items = sum([1, 2, 3, 4, 5, 6])
# sum_of_items will be equal to 21

SQL

SELECT SUM(column1) FROM MyTable
WHERE column1 = "condition"

C#

List<int> array = new List<int>() {1, 2, 3, 4, 5};
int sum = array.Sum();  # sum == 21

Java

import java.util.Arrays;
public class MyClass {
    public static void main(String args[]) {
      var numbers = Arrays.asList(1, 2, 3, 4, 5);
      var sum = numbers.stream().mapToInt(Integer::intValue).sum();
      System.out.println(sum);
    }
}

R

x <- c(1.1, 2.5, -0.4)
y <- c(NA, 1.7)
sum(x)
sum(y)
sum(y, na.rm = TRUE)
sum(x, y)  ## as same as sum(c(x, y))
sum(x, y, na.rm = TRUE)  ## as same as sum(c(x, y), na.rm = TRUE)
15351 questions
46
votes
5 answers

Torch sum a tensor along an axis

How do I sum over the columns of a tensor? torch.Size([10, 100]) ---> torch.Size([10])
Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142
45
votes
3 answers

SQL not a single-group group function

When I run the following SQL statement: SELECT MAX(SUM(TIME)) FROM downloads GROUP BY SSN It returns the maximum sum value of downloads by a customer, however if I try to find the social security number that that max value belongs to by adding it…
Tomek
  • 4,689
  • 15
  • 44
  • 52
44
votes
4 answers

Performing a query on a result from another query?

I have a the query: SELECT availables.bookdate AS Date, DATEDIFF(now(),availables.updated_at) as Age FROM availables INNER JOIN rooms ON availables.room_id=rooms.id WHERE availables.bookdate BETWEEN '2009-06-25' AND date_add('2009-06-25', INTERVAL 4…
holden
  • 13,471
  • 22
  • 98
  • 160
44
votes
6 answers

ignore NA in dplyr row sum

is there an elegant way to handle NA as 0 (na.rm = TRUE) in dplyr? data <- data.frame(a=c(1,2,3,4), b=c(4,NA,5,6), c=c(7,8,9,NA)) data %>% mutate(sum = a + b + c) a b c sum 1 4 7 12 2 NA 8 NA 3 5 9 17 4 6 NA NA but I like to get a b …
ckluss
  • 1,477
  • 4
  • 21
  • 33
44
votes
9 answers

Efficiently sum across multiple columns in R

I have the following condensed data set: a<-as.data.frame(c(2000:2005)) a$Col1<-c(1:6) a$Col2<-seq(2,12,2) colnames(a)<-c("year","Col1","Col2") for (i in 1:2){ a[[paste("Var_", i, sep="")]]<-i*a[[paste("Col", i, sep="")]] } I would like to sum…
user2568648
  • 3,001
  • 8
  • 35
  • 52
44
votes
9 answers

ruby: sum corresponding members of two or more arrays

I've got two (or more) arrays with 12 integers in each (corresponding to values for each month). All I want is to add them together so that I've got a single array with summed values for each month. Here's an example with three values: [1,2,3] and…
jjnevis
  • 2,672
  • 3
  • 22
  • 22
44
votes
12 answers

How to sum two fields in AngularJS and show the result in an label?

I have an situation on my page. I have two inputs and an label in my page. These label have to show the sum of these two inputs value. So I tried below solution: Sub-Total Tax
Aitiow
  • 882
  • 2
  • 9
  • 18
44
votes
5 answers

Sum of row n through last row

I want to create a TOTAL row at the top of my spreadsheet. In this row, each cell should be the SUM of the values in the column below the TOTAL row. So for example, if the total row is Row 1, cell A1 should be the SUM of A2 through the last row in…
Nick Petrie
  • 5,364
  • 11
  • 41
  • 50
43
votes
2 answers

Sum 2 hashes attributes with the same key

I have 2 hashes, for example: {'a' => 30, 'b' => 14} {'a' => 4, 'b' => 23, 'c' => 7} where a, b and c are objects. How can I sum those hashes' keys to get a new hash like: {'a' => 34, 'b' => 37, 'c' => 7}
el_quick
  • 4,656
  • 11
  • 45
  • 53
42
votes
3 answers

How do I sum the values in an array of maps in jq?

Given a JSON stream of the following form: { "a": 10, "b": 11 } { "a": 20, "b": 21 } { "a": 30, "b": 31 } I would like to sum the values in each of the objects and output a single object, namely: { "a": 60, "b": 63 } I'm guessing this will…
Alan Burlison
  • 1,022
  • 1
  • 9
  • 16
42
votes
5 answers

SELECT query with CASE condition and SUM()

I'm currently using these sql statements. My table has the field CPaymentType which contains "Cash" or "Check". I can sum up the amount of payments by executing 2 SQL statements as shown below. In this case, the user won't even notice the speed…
chris_techno25
  • 2,401
  • 5
  • 20
  • 32
42
votes
10 answers

Sum of list of lists; returns sum list

Let data = [[3,7,2],[1,4,5],[9,8,7]] Let's say I want to sum the elements for the indices of each list in the list, like adding numbers in a matrix column to get a single list. I am assuming that all lists in data are equal in length. print…
Albert
  • 505
  • 1
  • 5
  • 7
41
votes
11 answers

How to sum dict elements

In Python, I have list of dicts: dict1 = [{'a':2, 'b':3},{'a':3, 'b':4}] I want one final dict that will contain the sum of all dicts. I.e the result will be: {'a':5, 'b':7} N.B: every dict in the list will contain same number of key, value pairs.
Nazmul Hasan
  • 6,840
  • 13
  • 36
  • 37
40
votes
7 answers

Changing a SUM returned NULL to zero

I have a stored procedure as follows: CREATE PROC [dbo].[Incidents] (@SiteName varchar(200)) AS SELECT ( SELECT SUM(i.Logged) FROM tbl_Sites s INNER JOIN tbl_Incidents i ON s.Location = i.Location WHERE s.Sites =…
Icementhols
  • 653
  • 1
  • 9
  • 11
40
votes
6 answers

Django Aggregation: Sum return value only?

I have a list of values paid and want to display the total paid. I have used aggregation and Sum to calculate the values together. The problem is,I just want the total value printed out, but aggregation prints out: {'amount__sum': 480.0} (480.0…
Stephen
  • 527
  • 2
  • 7
  • 19