EzDevInfo.com

sum interview questions

Top sum frequently asked interview questions

How do I get SUM function in MySQL to return '0' if no values are found?

Say I have a simple function in MySQL:

$query="SELECT SUM(Column 1) from Table WHERE Column 2='Test'";

If no entries in Column 2 contain the text 'Test' then this function returns NULL, when I would like it to return 0. I'm aware that a similar question has been asked a few times here, but I haven't been able to adapt the answers to my purposes, so I'd be grateful for some help to get this sorted.

Thanks,

Nick


Source: (StackOverflow)

Linq query with nullable sum

from i in Db.Items
select new VotedItem
{
    ItemId = i.ItemId,
    Points = (from v in Db.Votes
              where b.ItemId == v.ItemId
              select v.Points).Sum()
}

I got this query, however it fails if no votes are found with exception:

The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.

I assume its because sum returns an int and not a nullable int, giving sum a int? as input only give the same error, probably cause sum only workes on ints.

Any good workaround for this?


Source: (StackOverflow)

Advertisements

Get sum of two columns in one LINQ query

let's say that I have a table called Items (ID int, Done int, Total int)

I can do it by two queries:

int total = m.Items.Sum(p=>p.Total)
int done = m.Items.Sum(p=>p.Done)

But I'd like to do it in one query, something like this:

var x = from p in m.Items select new { Sum(p.Total), Sum(p.Done)};

Surely there is a way to call aggregate functions from LINQ syntax...?


Source: (StackOverflow)

Add SUM of values of two LISTS into new LIST

I have the following two lists:

first = [1,2,3,4,5]
second = [6,7,8,9,10]

Now I want to add items of both lists into a new list.

output should be

three = [7,9,11,13,15]

Source: (StackOverflow)

How do you find the sum of all the numbers in an array in java?

I'm having a problem finding the sum of all of the integers in an array in java. Cannot find any useful method in the Math class for this.


Source: (StackOverflow)

How to sum all values of a column of in a data.frame?

I have a data frame with several columns; some numeric and some character. How to compute the sum of a specific column? I’ve googled for this and I see numerous functions (sum, cumsum, rowsum, rowSums, colSums, aggregate, apply) but I can’t make sense of it all.

For example suppose I have a data frame people with the following columns

Name Height Weight
Mary 65     110
John 70     200
Jane 64     115
…

How do I get the sum of all the weights?


Source: (StackOverflow)

Sum range of int's in List

I reckon this will be quite trivial but I can't work out how to do it. I have a List<int> and I want to sum a range of the numbers.

Say my list is:

var list = new List<int>()
{
    1, 2, 3, 4
};

How would I get the sum of the first 3 objects? The result being 6. I tried using Enumerable.Range but couldn't get it to work, not sure if that's the best way of going about it.

Without doing:

int sum = list[0] + list[1] + list[2];

Source: (StackOverflow)

Getting N random numbers that the sum is M

I want to get N random numbers that the sum of them is a value.

For example, let's suppose I want 5 random numbers that their sum is 1

Then, a valid possibility is:

0.2 0.2 0.2 0.2 0.2

Other possibility is:

0.8 0.1 0.03 0.03 0.04

And so on. I need this for the creation of the matrix of belongings of the Fuzzy C-means.


Source: (StackOverflow)

How to calculate sum of a DataTable's Column in LINQ (to Dataset)?

I'm just started to read up on LINQ and I want to start incorporating it into my code. I know how to compute the sum of a DataTable's column by either "Foreach"-ing through the rows or by doing a compute.sum on the specific column. How do I do the equivalent with LINQ to DataSet?


Source: (StackOverflow)

How to sum array of numbers in Ruby?

I have an array of integers.

For example:

array = [123,321,12389]

Is there any nice way to get the sum of them?

I know, that

sum = 0
array.each { |a| sum+=a }

would work.


Source: (StackOverflow)

C# List of objects, how do I get the sum of a property

I have a list of objects. One property of the individual object entry is amount. How do I get the sum of amount?

If my list was of type double I may be able to do something like this:

double total = myList.Sum();

However I want to something similar to this, yet this syntax is incorrect.

double total = myList.amount.Sum();

How should I go about accomplishing this? I would love to use the Sum function if possible instead of looping through and calculating the value.


Source: (StackOverflow)

Using GroupBy, Count and Sum in LINQ Lambda Expressions

I have a collection of boxes with the properties weight, volume and owner.

I want to use LINQ to get a summarized list (by owner) of the box information

e.g.

**Owner, Boxes, Total Weight, Total Volume**  
Jim,     5,     1430.00,      3.65  
George,  2,     37.50,        1.22

Can someone show me how to do this with Lambda expressions?


Source: (StackOverflow)

Sum of all values in a Python dict

I'm new to Python. Let's say I have a dictionary in which the keys map to integers like:

d = {'key1':1,'key2':14,'key3':47}

Is there a syntactically minimalistic way to return the sum of the values in d--i.e. 62 in this case.

Thanks


Source: (StackOverflow)

SQL Update to the SUM of its joined values

I'm trying to update a field in the database to the sum of its joined values:

UPDATE P
SET extrasPrice = SUM(E.price)
FROM dbo.BookingPitchExtras AS E
INNER JOIN dbo.BookingPitches AS P ON E.pitchID = P.ID
    AND P.bookingID = 1
WHERE E.[required] = 1

When I run this I get the following error:

"An aggregate may not appear in the set list of an UPDATE statement."

Any ideas?


Source: (StackOverflow)

How to sum properties of the objects within an array in Ruby

I understand that in order to sum array elements in Ruby one can use the inject method, i.e.

array = [1,2,3,4,5];
puts array.inject(0, &:+) 

But how do I sum the properties of objects within an object array e.g.?

There's an array of objects and each object has a property "cash" for example. So I want to sum their cash balances into one total. Something like...

array.cash.inject(0, &:+) # (but this doesn't work)

I realise I could probably make a new array composed only of the property cash and sum this, but I'm looking for a cleaner method if possible!


Source: (StackOverflow)