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
28
votes
2 answers

Pandas groupby nlargest sum

I am trying to use groupby, nlargest, and sum functions in Pandas together, but having trouble making it work. State County Population Alabama a 100 Alabama b 50 Alabama c 40 Alabama d 5 Alabama e …
user7102752
  • 281
  • 1
  • 3
  • 4
28
votes
8 answers

SUM(DISTINCT) Based on Other Columns

I currently have a table that looks something like this: +------+-------+------------+------------+ | id | rate | first_name | last_name | +------+-------+------------+------------+ What I need to do is get the SUM of the rate column, but only…
David
  • 3,831
  • 2
  • 28
  • 38
27
votes
3 answers

How to prevent arithmetic overflow error when using SUM on INT column?

I am using SQL Server 2008 R2 and I have an INT column where the data inserted never surpasses the max INT, but I have a query which uses the SUM function which when executed surpasses the max INT limit and throws the error mentioned in the title. I…
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
27
votes
1 answer

Collectors.summingInt() vs mapToInt().sum()

When you want to sum an integer value from a stream, there are two main ways of doing it: ToIntFunction<...> mapFunc = ... int sum = stream().collect(Collectors.summingInt(mapFunc)) int sum = stream().mapToInt(mapFunc).sum() The first involves…
thecoop
  • 45,220
  • 19
  • 132
  • 189
27
votes
7 answers

Google Interview: Find all contiguous subsequence in a given array of integers, whose sum falls in the given range. Can we do better than O(n^2)?

Given an array of Integers, and a range (low, high), find all contiguous subsequence in the array which have sum in the range. Is there a solution better than O(n^2)? I tried a lot but couldn't find a solution that does better than O(n^2).…
user1071840
  • 3,522
  • 9
  • 48
  • 74
27
votes
11 answers

how to count the total number of lines in a text file using python

For example if my text file is: blue green yellow black Here there are four lines and now I want to get the result as four. How can I do that?
user2794146
27
votes
7 answers

How to add column values in mysql

This is my table data Student And this is my query -- SELECT id, SUM( maths + chemistry + physics ) AS total, maths, chemistry, physics FROM `student` but it is throwing a single row -- id total maths chemistry physics 118 760 55 …
Trialcoder
  • 5,816
  • 9
  • 44
  • 66
27
votes
5 answers

JavaScript SUM and GROUP BY of JSON data

This is my first attempt at doing JavaScript with some JSON data objects and need some advice on the proper way to attain my goal. Some server-side code actually generates a JSON formatted string that I have to work with and assign it to a…
LordChariot
  • 543
  • 1
  • 6
  • 10
27
votes
3 answers

How can I use SUM() OVER()

I can't understand this code's bug ID AccountID Quantity 1 1 10 Sum = 10 2 1 5 = 10 + 5 = 15 3 1 2 = 10 + 5 + 2 = 17 4 2…
serkan
  • 385
  • 1
  • 4
  • 4
26
votes
2 answers

SSRS Conditional Summing

I have an SSRS report that displays several pages of rows. In each row is a "TYPE" field. In that TYPE field there is either an "M" for the value or a "P" for the value. At the end of the report I want to summ up all the price values for the "P"…
MikeTWebb
  • 9,149
  • 25
  • 93
  • 132
26
votes
5 answers

Index a sum column

Is creating an index for a column that is being summed is faster than no index?
user26087
  • 809
  • 2
  • 13
  • 21
26
votes
8 answers

Sum all the digits of a number Javascript

I am newbie. I want to make small app which will calculate the sum of all the digits of a number. For example, if I have the number 2568, the app will calculate 2+5+6+8 which is equal with 21. Finally, it will calculate the sum of 21's digits and…
Hodorogea Alexandru
  • 449
  • 2
  • 5
  • 11
25
votes
5 answers

MySQL select SUM of results with a LIMIT

I have a table full of items and prices for said items. I would like to grab the SUM of the 3 highest priced items. I thought perhaps SELECT SUM(items.price) FROM items ORDER BY items.price LIMIT 3 but that does not seem to do the trick. Is this…
PotatoFro
  • 6,378
  • 4
  • 20
  • 22
25
votes
3 answers

With pairwise summation, how many terms do I need to get an appreciably wrong result?

Using a given species of fp numbers, say float16, it is straight forward to construct sums with totally wrong results. For example, using python/numpy: import numpy as np one = np.float16(1) ope =…
Paul Panzer
  • 51,835
  • 3
  • 54
  • 99
25
votes
5 answers

Concatenate tuples using sum()

From this post I learned that you can concatenate tuples with sum(): >>> tuples = (('hello',), ('these', 'are'), ('my', 'tuples!')) >>> sum(tuples, ()) ('hello', 'these', 'are', 'my', 'tuples!') Which looks pretty nice. But why does this work? …
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135