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
25
votes
6 answers

Is it possible to speed up a sum() in MySQL?

I'm doing a "select sum(foo) from bar" query on a MySQL database that's summing up 7.3mm records and taking about 22 seconds per run. Is there a trick to speeding up sums in MySQL?
Teflon Ted
  • 8,696
  • 19
  • 64
  • 78
25
votes
1 answer

MySQL using Sum and Case

I'm trying to create a GridView with ASP.NET connecting to a MySQL database. The data appears like below. BusinessUnit OrderDate Canceled UnitA 1/15/2013 N UnitA 10/1/2013 N UnitB 10/15/2013 …
Latex Person
  • 355
  • 2
  • 5
  • 10
24
votes
1 answer

Weird numpy.sum behavior when adding zeros

I understand how mathematically-equivalent arithmentic operations can result in different results due to numerical errors (e.g. summing floats in different orders). However, it surprises me that adding zeros to sum can change the result. I thought…
shx2
  • 61,779
  • 13
  • 130
  • 153
24
votes
5 answers

How to Multiply all values within a column with SQL like SUM()

Lets say I have table with 1 column like this: Col A 1 2 3 4 If I SUM it, then I will get this: Col A 10 My question is: how do I multiply Col A so I get the following? Col A 24
Tommy Sayugo
  • 375
  • 2
  • 5
  • 16
23
votes
3 answers

A PowerShell script to find the file size and file count of a folder with millions of files?

The purpose of the script is the following: Print the number of files recursively found within a directory (omitting folders themselves) Print the total sum file size of the directory Not crash the computer because of massive memory use. So far…
Stephen Wood
  • 1,257
  • 1
  • 12
  • 13
23
votes
5 answers

Sum of antidiagonal of a matrix

I'm trying to sum the elements along the antidiagonal (secondary diagonal, minor diagonal) of a matrix. So, if I have a matrix m: m <- matrix(c(2, 3, 1, 4, 2, 5, 1, 3, 7), 3) m [,1] [,2] [,3] [1,] 2 4 1 [2,] 3 2 3 [3,] 1 …
Windstorm1981
  • 2,564
  • 7
  • 29
  • 57
23
votes
3 answers

Count Returning blank instead of 0

Good day everyone. Here is my code: SELECT 'Expired Item -'+ DateName(mm,DATEADD(MM,4,AE.fld_LOAN)) as [Month] ,COUNT(PIT.fld_ID)'COUNT' ,SUM (PIT.fld_GRAM)'GRAMS' ,SUM (PH.fld_AMNT)'PRINCIPAL' FROM #AllExpired AE INNER JOIN…
Albert Laure
  • 1,702
  • 5
  • 20
  • 49
23
votes
9 answers

Sum every nth points

I have a vector and I need to sum every n numbers and return the results. This is the way I plan on doing it currently. Any better way to do this? v = 1:100 n = 10 sidx = seq.int(from=1, to=length(v), by=n) eidx = c((sidx-1)[2:length(sidx)],…
Alex
  • 19,533
  • 37
  • 126
  • 195
23
votes
4 answers

Sum with SQL server RollUP - but only last summary?

I have this query: DECLARE @t TABLE(NAME NVARCHAR(MAX),datee date,val money) insert INTO @t SELECT 'a','2012-01-02',100 insert INTO @t SELECT 'a','2012-01-02',100 insert INTO @t SELECT 'a','2012-01-03',100 insert INTO @t SELECT…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
23
votes
9 answers

How get total sum from input box values using Javascript?

I am not perfect in Javascript.. I want to show total sum of values entered in qty input boxes in next input box named total without refreshing page. Can anyone will help me to figure it out..? Here is javascript