sum

Strange problem with ActiveRecord summing grouped values

I'm working on a timesheet system (thrilling, I know) and am having difficulty using ActiveRecord to total the time spent working on particular projects on each day. I have a TimeBooking model: t.references :assignment, :null => false # (Combination of employee and project) t.date :date, :null => false t.integer :durat...

Getting N random numbers that the sum is M

Hello 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. ...

Zend Framework how to echo value of SUM query

Hello, I created a query for the zend framework, in which I try to retrieve the sum of a column, in this case the column named 'time'. This is the query I use: $this->timequery = $this->db_tasks->fetchAll($this->db_tasks->select()->from('tasks', 'SUM(time)')->where('projectnumber =' . $this->value_project)); $this->view->sumtime = $t...

how can i get the total from the four different table in single query

In each table have many number of rows with amount. Like this i have different numbers of amount in each table. Each tables field name is different. How can i get the total of all values in the four tables in a single query? Any way is there? ...

select SUM in Cake

When I tried using follow code to get the sum of the column named Serviceinfo.services $conditions = array('fields'=>array('SUM(Serviceinfo.services) as servicecount'),'conditions'=>array('time_id BETWEEN ? AND ?'=>array($startid,$endtid))); $services = $this->Serviceinfo->find('all',$conditions); I had to fetch the sum data with serv...

Math: How to sum each row of a matrix

I have a 1x8 matrix of students where each student is a 4x1 matrix of scores. Something like: SCORES S [62, 91, 74, 14] T [59, 7 , 59, 21] U [44, 9 , 69, 6 ] D [4 , 32, 28, 53] E [78, 99, 53, 83] N [48, 86, 89, 60] T [56, 71, 15, 80] S [47, 67, 79, 40] Main question: Using sigma notation, or some other mathematical function, h...

PHP: find two or more numbers from a list of numbers that add up towards a given amount

Hi Guys, I am trying to create a little php script that can make my life a bit easier. Basically, I am going to have 21 text fields on a page where I am going to input 20 different numbers. In the last field I will enter a number let's call it the TOTAL AMOUNT. All I want the script to do is to point out which numbers from the 20 fields...

Complicated SQL query

Please bare with me this is difficult to explain xD http://img90.imageshack.us/i/croppercapture1.png/ This is based on an undergraduate degree course where a student takes many units (say 4 core units and 1 optional unit per year). tblAwardCoreUnits and tblAwardOptUnits store which units are optional and core for which award, hence the...

given a list of up to 10 integer numbers and a sum, will display a subset of the numbers whose total is that sum

Write a program which, given a list of up to 10 integer numbers and a sum, will display a subset of the numbers whose total is that sum if one exists or indicate that none exists otherwise. For example, for the list: 5,13,24,9,3,3 and sum = 28, your program should display 13, 9, 3, 3. How to do this in C++ using a recursive function? ...

I can't make this query work with SUM function

This query gives an error: select ep, case when ob is null and b2b_ob is null then 'a' when ob is not null or b2b_ob is not null then 'b' else null end as type, sum(b2b_d + b2b_t - b2b_i) as sales from table where ... group by ep, type Error: ORA-00904: "TYPE": invalid identifier When I run it with group by ep, the...

ruby: sum corresponding members of two 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 [4,5,6] => [5,7,9] The best I could come up with was: [[1,2,3],[4,5,6]].transpose.map{|ar...

Cocoa @sum array operator too slow - alternatives?

I've got a text field value bound to a key path that uses @sum. When the array controller is filtered, the text field updates properly, but extremely slowly, lagging the UI or outright beachballing after every key press (less so as the number of items in arrangedObjects decreases). There is no lag if I don't use @sum; I've narrowed it do...

Time calculations with MySQL TIMEDIFF

Hi there, I have the following table: mysql> SELECT id,start1,stop1,start2,stop2 FROM times; +----+---------------------+---------------------+---------------------+---------------------+ | id | start1 | stop1 | start2 | stop2 | +----+---------------------+---------------------+----...

MySQL count/sum fields

Hi There, What I am trying to achieve is a report on daily financial transactions. With my SQL query I would like to count the total number of cash transactions, the total cash value and the same for checks. I only want to do this for a specified date. Here is a snippet of the query that I am having trouble with. These sum and count co...

Complex Join - involving date ranges and sum...

I have two tables that I need to join... I want to join table1 and table2 on 'id' - however in table two id is not unique. I only want one value returned for table two, and this value represents the sum of a column called 'total_sold' - within a specified date range (say one month), however I want more than one date range at the same tim...

How would I find all sets of N single-digit, non-repeating numbers that add up to a given sum in PHP?

Let's say I want to find all sets of 5 single-digit, non-repeating numbers that add up to 30... I'd end up with [9,8,7,5,1], [9,8,7,4,2], [9,8,6,4,3], [9,8,6,5,2], [9,7,6,5,3], and [8,7,6,5,4]. Each of those sets contains 5 non-repeating digits that add up to 30, the given sum. Any help would be greatly appreciated. Even just a starting...

SQL query: SUM values up to the current record

Basically, having this table: 12.10 2.35 21.45 35.26 I want to, in each record, calculate the sum of all previous records, like this: 12.10 | 12.10 2.35 | 14.45 21.45 | 35.90 35.26 | 71.16 ...

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 = @SiteName AND i.[month] = DATEADD(mm, DATEDIFF(mm, 0, GetDate()) -1,0) GROUP BY s.Site...

MySQL join two tables and add column values

My knowledge of MySQL is not very in depth. If I have two tables that for example look like this: Table1 Date v1 v2 v3 05/01/2010 26 abc 45 05/02/2010 31 def 25 05/03/2010 50 ghi 46 Table2 Date v1 v2 v3 05/01/2010 42 jkl 15 05/02/2010 28 mno 14 05/03/2010 12 pqr 64 How can I join them in a query by their dat...

How do I sum values from two dictionaries in C#?

I have two dictionaries with the same structure: Dictionary<string, int> foo = new Dictionary<string, int>() { {"Table", 5 }, {"Chair", 3 }, {"Couch", 1 } }; Dictionary<string, int> bar = new Dictionary<string, int>() { {"Table", 4 }, {"Chair", 7 }, {"Couch", 8 } }; I'd like to sum the values of the dictiona...