sum

Fast sum of values in a multidimensional array (C#)

With a 1D array, I can use the sum method to get the sum of all the values. int[] array = {6,3,1}; Console.WriteLine(array.Sum()); With a multidimensional array (3D in my case), this can't be done. Obviously I could go all foreach on it, but this seems verbose and I suspect it will perform badly. Is there a way to flatten the array? ...

Sum of tagged lines perl/shellscript

I have input that looks like 5 X 8 Y 3 Z 9 X I want output that sums the numerical values for each 'tag'; e.g. 14 X 8 Y 3 Z Wondering if there is a slick one liner I can use (along the lines of the ones for summing a list of integers using awk). ...

SQL Server: how do I SUM column values if the name of the column contains a word?

In my SQL table: Period| Brand A small Bags| Brand A big bags| Brand D Shoes| ...| Brand X Shoes 2010 | 10 | 20 | 30 | ...| 200 How do I sum columns that contains certain words (e.g shoes) in the column names ? Expected results: Period | Sum of Bags | Sum of Shoes | .. 2010 | 30 ...

Sum properties using LINQ

DealsThisMonthOpen = deals.Where(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Open").Count(), DealsThisMonthLost = deals.Where(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Lost").Count(), DealsThisMonthWon = deals.Where(deal => deal.DateCreated.Month == date.Month && deal.DealStatus == "Won")...

Calculate Sums with accumulate

procedure accumulate is defined like this: (define (accumulate combiner null-value term a next b) (if (> a b) null-value (combiner (term a) (accumulate combiner null-value term (next a) next b)))) problem 1: x^n ;Solution: recursive without accumulate (define (expon x n) (if (> n 0) (* x (ex...

TSQL Summary by account, what is the cleanest way to approach this?

I am trying to total by account, the expenses & income per account. There are multiples of both the income & the expenses per account. I am struggling with this as am still learning SQL and thought that someone else likely has already addressed this? I sure would appreciate the help! I know that this SQL server code is not correct bu...

How to group summed up columns from different tables?

I have a user table (User) and 3 tutorial tables (Text, Video and Other). Each tutorial has the columns rating_positive and rating_negative and is linked to a user (id). I want to select the 10 users with the most tutorials and the sum of positive/negative ratings of their tutorials. I tried the following query but it does not work. I...

MySQL / Ruby on Rails - How to "SUM" in a :has_many case

Hi there, I have the following tables: User :has_many Purchases Item :has_many Purchases Item has a column "amount" (can be + or -) and I need to find all Users that have a positive SUM of "Item.amounts" (over all Purchases each one has made). How does this query look like? (I'm not sure how to handle "SUM" correctly, in this c...

How to count records from multiple columns using sql and asp.net?

I have a table with 5 columns: ID, Session1, Session2, Session3, Session4. What I want to do is to display the number of same items from each column. Same item can't appear in more than one column. ID Session1 Session2 Session3 Session4 -- -------- -------- -------- -------- 1 Music Web Tech Fu...

Any 'other' way to SUM variables using PHP?

In a form I have, a consumer can choose to add a blood test, a vision test, or both (combo) to several enrollee's accounts. This is all done at one time. Based on the choice made, I am able to select the 'choice' and echo a respective price but am having the hardest time building a script to 'add or sum' all the variables together. I a...

Get the sum of digits in PHP

How do I find the sum of all the digits in a number in PHP? I've found solutions for C# and others, but not PHP. How do I do it? Thanks! ...

SQL for summing only new values

Hi, i have a table like this NAME VALUE ----------------- bla 1 bla 2 bla 2 bla 3 bla 1 bla 4 bla 2 How can i do a sum of ONLY different values , and ignore the repeating values (is it possible?)? Something like this : SELECT SUM(??condition?? value) as total FROM table And the sum should be...

Algorithm to find the maximum sum in a sequence of overlapping intervals

The problem I am trying to solve has a list of intervals on the number line, each with a pre-defined score. I need to return the maximum possible total score. The catch is that the intervals overlap, and of the overlapping intervals I can use only one. Here is an example. Intervals - Score 0- 5 - 15 4- 9 - 18 ...

PHP: Sum list of values

My page should shown information like this: No. Customer Bank_Name Amount --------------------------- 1. AAA Bank A 100 2. BBB Bank B 200 3. CCC Bank A 500 4. DDD Bank C 150 ---------------------------- Total 950 How to sum amount as shown below: Summary No.Bank_Name...

Getting sum() on a different distinct row MySQL

Hello, I was looking on different questions on this issue, but couldn't find an answer for my problem. This is my query: SELECT SUM( lead_value ) AS lead_value_sum, count( DISTINCT phone ) AS SUM, referer FROM leads t1 INNER JOIN leads_people_details t2 ON t1.lead_id = t2.lead_id INNER JOIN user_to_leads t3 ON t1.lead_id = t3.lead_id ...

Array sum of value based on same key

I have my array data as shown below: Array ( [0] => Array ( [name] => Bank BRI [amount] => 0 ) [1] => Array ( [name] => Bank BRI [amount] => 0 ) [2] => Array ( [name] => Bank BCA [amount] => 1412341234 ) [3] => Array ( [name] => Bank CIMB Niaga [amount] => 532532552 ) [4] => Array ( [name] => Bank BRI [amount] => 34534534 ...

SQL - SUM of fields by alias

Hi, I'm trying to find the sum of all the listed fields, but given that they are sub queries... I need to use aliases. If i use the listed aliases, I get column/field unknown errors... and if I attempt a sum(points) using a group by, I get an invalid use of group clause error. SELECT DISTINCT pr.competitorID AS compID, pr.age, CONCAT(p...

MySQL: How to "sum" product weight if the product are in different lines

I have to sum the weight of the certain products, but they are in different lines of MySQL. How to do it? Here is an example of my database: ID | Product_id | weight_kg | Quantity | 1 | 201 | 6 | 3 | 2 | 102 | 3 | 1 | 3 | 103 | 4 | 4 | 4 | 200 | 2 ...

Summing Non-Integers in Python sum([[1],[2]]) = [1,2]

Is it possible to take the sum of non-integers in python? The command sum([[1],[2]]) for example, gives the error Traceback (most recent call last): File "<pyshell#28>", line 1, in <module> sum([[1,2,3],[2,3,4]]) TypeError: unsupported operand type(s) for +: 'int' and 'list' I suspect sum tries to add 0 to the list [1], resu...

Counting all other types but the current one

Hi, I'm trying to write this query, that would calculate the average value of all the columns except the one that contains the type value, which I'm grouping the whole query by. So for 4 types for example, each column in the resulting table will contain the average of all the other three type's values, i need to exclude the current type...