FAQ Database Discussion Community

## Select organizations that their income represent around 60% of the total income SQL Server2008

sql-server,sum,data-warehouse,percentage
In advance, I apologize for imperfect English In this data warehouse, we have organization which composed of multiple Organizations, I have [FactFinance] table which has information about the income of each organization. I have the following query in data warehouse which select the (Organization Name) from the [organization dimension table]...

## Subtract two columns of different tables

sql,postgresql,sum,aggregate-functions,subtract
I have two unrelated tables: contribution(id,amount, create_at, user_id) solicitude(id, amount, create_at, status_id, type_id, user_id) I need to subtract the sum of the amount of the contribution and of the solicitude from a user, but that result can't to be negative. How can I do this? Function or query? I tried...

## How to know which checkbox are selected if I have a number with the sum of all values checked?

c#,checkbox,sum
I have an existing database, and my table has a field in which is stored a value, a sum of all "values" of my checkbox list. Another programmer made this method, called it "logic sum". I don't know this method, but now I need to know the reverse method, so...

## Select left 10 numbers, left join for a price from second table, and then sum, SQL

sql,select,sql-server-2012,sum,multiplying
I am currently working in sql 2012 visual management studio. I have two tables. Table1 has three columns (ItemNumber as varchar, Quantity as int, and TimeOrdered as datetime). Table2 has 2 columns (ItemNumber as varchar, and Price as float). Please note these item numbers are not the same, the part...

## SUM case returns value without GROUP BY

mysql,sql,group-by,sum,case
When I add SUM around my case select, it returns the summed value without the GROUP BY. The query I am using, without the SUM, is the following SELECT CASE WHEN subscription_types.type = 'Succes lidmaatschap' THEN 7 ELSE 8 END FROM subscription_used INNER JOIN training_sessions ON training_sessions.id = subscription_used.training_session_id INNER...

## How to Calculate Aggregated Product Function in SQL Server

sql,sql-server,sum,aggregate-functions,exp
I have a table with 2 column: No. Name Serial 1 Tom 1 2 Bob 5 3 Don 3 4 Jim 6 I want to add a column whose a content is multiply Serial column like this: No. Name Serial Multiply 1 Tom 2 2 2 Bob 5 10 3...

## Combine multiple dataframes into one that sums their values according to the index

python,pandas,sum
I have several pandas dataframes, each with one column of ints in them, and I would like to create a new dataframe with the sum of their values at each index. Their indexes will have some overlapping entries and these are the indecies whose values I want to add together....

## Sum of all subparts of an array of integers

arrays,algorithm,sum,big-o,subset
Given an array {1,3,5,7}, its subparts are defined as {1357,135,137,157,357,13,15,17,35,37,57,1,3,5,7}. I have to find the sum of all these numbers in the new array. In this case sum comes out to be 2333. Please help me find a solution in O(n). My O(n^2) solution times out. link to the problem...

## How can I use target address to get sum of a range of cells? [closed]

excel,vba,sum,range,target
I have target.address (or just target) from a worksheet_change sub. I'd like to use the row from target.address and a range of columns (H:W), and get a sum of that range. So, for instance, if I have $100 in H10 and I add$50 in J10, I'd like to get...

## How to sum items from csv files from a for loop

python,loops,csv,sum
I am stuck. My script asks for how many csv files a user wants to enter and by what field. In this case lets say two csv files each with a area value. I am trying to sum up the two items with a for loop so i can then...

## Is this working properly - Sum of Fibonacci in Python 3

python,numbers,sum,fibonacci
I have a task to make a program that will sum the first 100 Fibonacci numbers. I checked my output in Python, and my output in QBasic 64 and they aren't same. I checked with different inputs also. Input: 10 Output: 89 ----------- Input: 100 Output: 573147844013817084101 Is it correct...

## Using Sum in If in Mysql

mysql,sql,select,sum
I want to select the sum of values of my_field. The unit of my_field is in seconds. I don't want to exceed 24 hours, so when it exceeds 24 hours, I want it to select 24*60*60 = 86400. SELECT IF(SUM(my_field) > 86400, 86400, SUM(my_field)) ... This solution doesn't seem to...

## Given a sorted array and a parameter k, find the count of sum of two numbers greater than or equal to k

c++,arrays,algorithm,sum
I tried to find all pairs in the array with sum equal to k, the solution to that takes O(n*log(n)) time. Here is the code snippet - map<int,int> mymap; map<int,int>::iterator it; cin>>n>>k; for( int i = 0 ; i < n ; i++ ){ cin>>a; if( mymap.find(a) != mymap.end() )...

## Percentage for each result where id = id

php,html,for-loop,mysqli,sum

## Subquery aggregate function with SUM(CASE SUBQUERY)

sql,sql-server,sum,subquery,aggregate-functions
I'm getting an error while executing my query Cannot perform an aggregate function on an expression containing an aggregate or a subquery. Code: SELECT S.id, SUM(CASE WHEN sc.coverage IN (SELECT number FROM ArrayOfIntegersFromString(@dynamicData)) THEN 1 ELSE 0 END) as sm FROM Storefronts s LEFT JOIN StorefrontCoverages sc ON s.id =...

## SQL Server : Sum by Row to output

sql-server,sum,row
Trying to display sum of same rows as output. Here is SQL code SELECT replace(replace(replace(replace(dbo.IDENTITY_MAP.Name,'My Company\',''),'-VLAN2',''),'.VLAN2\',''),'.Instr\','') as Site, Count (CASE WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '11.%' AND dbo.SEM_COMPUTER.OPERATION_SYSTEM LIKE 'Windows%' THEN 1 END) AS 'Windows-SEP-11', Count (CASE WHEN dbo.SEM_AGENT.AGENT_VERSION LIKE '11.%' AND dbo.SEM_COMPUTER.OPERATION_SYSTEM LIKE 'Mac%' THEN 1 END) AS 'Mac-SEP-11', Count(CASE WHEN...

## Extracting raster values, from maximum, to cumulatively sum to x

r,sum,raster
I am trying to determine the location of raster cells that add up to a given amount, starting with the maximum value and progressing down. Eg, my raster of 150,000 cells has a total sum value of 52,000,000; raster1 <- raster("myvalues.asc") cellStats(raster1,sum) = 52,000,000 I can extract the cells above...

## data.table sum and subset

r,sum,filtering,data.table
I have a data.table that I am wanting to aggregate library(data.table) dt1 <- data.table(year=c("2001","2001","2001","2002","2002","2002","2002"), group=c("a","a","b","a","a","b","b"), amt=c(20,40,20,35,30,28,19)) I am wanting to sum the amt by year and group and then filter where the summed amt for any given group is greater than 100. I've got the data.table sum nailed. dt1[, sum(amt),by=list(year,group)]...

## java8 stream grouping aggregate

sum,java-8,grouping,java-stream
Given a java class Something class Something { String parent; String parentName; String child; Date at; int noThings; Something(String parent, String parentName, String child, Date at, int noThings) { this.parent = parent; this.parentName = parentName; this.child = child; this.at = at; this.noThings = noThings; } String getParent() { return parent;...

## R - Compute increments for use in power variation

r,sum,binomial-coefficients
I am working with high frequent data, which is a vector X of decimal numbers. I would like to code the following in R, but am at complete loss where to start: n decides how big intervals we are looking at. This can be any value, this is not so...

## SUM previous value in php

php,mysql,sum
How can I create a function which can SUM the previous row with the new row and show me the result per each row?. Like this in the column"SALDO": | Cantidad | nombre del producto | Precio compra | Precio DNM | Precio venta | Total compra | Saldo |...

## PHP, MySQL How to Sum data in two different date range

php,mysql,sum
Im having trouble doing this +------------+------------+------------+ | Name | year | Value | +------------+------------+------------+ | Name1 | 2015 | 100 | | Name1 | 2014 | 150 | | Name1 | 2013 | 200 | | Name2 | 2015 | 130 | | Name2 | 2014 | 160 | |...

## How to perform boolean addition on an SQL select

sql,sql-server,select,sum,boolean
I have the following data in a database table column named timeSchedule 00100110 00010100 00110000 00110011 Boolean addition would result in 00110111 Is there a way to do this in sql? Something like select sumboolean(timeSchedule) from myTable Someone asked for DDL+DML.. here is an example: CREATE TABLE [dbo].[myTable]( [musPracticeID] [int]...

## Scala: How to compute the sum of all elements in the leaves of a Binary Tree?

scala,sum,binary-tree,immutability,case-class
I'm learning Scala by working the exercises from the book "Scala for the Impatient". Given the following way to model binary trees with case classes, one question asks to compute the sum of all elements in the leaves. sealed abstract class BinaryTree case class Leaf(value: Int) extends BinaryTree case class...

## countif and sumif each row without grouping

sql,sum,countif
I'm trying to figure out sumif and countif using SQL Here is the code to start with: SELECT ItemID , Supplier , Cost , SalePrice FROM countifExpirement I'm trying to create this output: First four columns already have data and i'm trying to create calculated fields to calculate duplicate Items,...

## Count the maximum amount of numbers in an array which sum is an even number

java,arrays,performance,sum
I have this code, which, as I understand it, searches for the maximum amount of consecutive numbers in the given array, which sum is an even number. private static int f (int[]a, int low, int high) { int res = 0; for (int i=low; i<=high; i++) res += a[i]; return...

## MySQL select sum then two grouping

php,mysql,sum,where,having
I have a table with id, name, type, price, name, date and type_2. Theses are bills. What do I have to query if I want to select all the bills between First May and 6 june that are to Mr. X. My code so far: SELECT sum(prix) from notes_de_frais WHERE...

## sum of Digits through Recursion

java,function,recursion,sum,digit
I am trying to make a recursive function which should return the sum of digits. The function should have only one argument. So far I have this; public int sumDigits(int n) { if(n%10 == n) // last digit remains return n; else{ int rightdigit; rightdigit = n%10; // taking out...

## sum up each individual values from 2 arrays

javascript,arrays,sum,element
I'm trying to understand this conditional loop to sum up each elements from 2 arrays, but I stumble upon the latter part, I don't understand what is to be achieved there. Can you please explain to me? function Arrays_sum(array1, array2) { var result = []; var ctr = 0; var...

## Expression can not contain lambda expression Linq

c#,linq,lambda,sum
I have data-table and i am trying to find sum of columns where Id starts with Particular value.I have tried some ways but getting error. result=Convert.ToInt32(dtNew.Compute("Sum(ResPending)", "Substring(ID,0,1)='G'"));//error shows-- Substring() argument is out of range And tried this way also dtNew.AsEnumerable().Where(x => x.Field<string>("ID").ToString().StartsWith("G"));//Expression can not contain lambda expression ...

## How do I find the sum of every X rows in a matrix in Matlab

matlab,sum,submatrix
I have a 189 x 4914 matrix and am trying to find the sum of each row in each 1x26 sub-matrix. How would I go about this? Many thanks. ...

## Return 0 in sum when no values to sum - sql server

sql,sql-server,sql-server-2008-r2,sum
Trying to return 0 if any of these columns have no values returned, in my particular case 'Past Due' has no values to total, so it is returned, so i get the return in the second snippet here. How can I return something if these are no values to count?...

## SUM Column SQL SERVER

sql-server-2008,sum
I have a table in SQL Server, and I need to sum a column, like the example below: CREATE TABLE B ( ID int, Qty int, ) INSERT INTO B VALUES (1,2) INSERT INTO B VALUES (2,7) INSERT INTO B VALUES (3,2) INSERT INTO B VALUES (4,11) SELECT *, ''...

## Python 3.x - Using the sum function to concatenate strings in a list

python,python-3.x,sum,concatenation
x= ['Good', 'morning'] # example list of strings I have to use sum(x) to concatenate both and print (x). I'm having trouble converting the list into integers for the sum function to work. >>>print (sum(x)) 'Goodmorning' ...

## Dynamic Summing Range

excel,vba,sum,excel-2010
Currently I have a medical spread-sheet with a list of clients that we have serviced. We have 8 different clinical categories which are denoted by different acronyms - HV,SV,CV,WV,CC,OV,TS and GS. A client can receive multiple therapies i.e. HV,SV,CV - in the background we have a counter mechanism which would...

## SQL COUNT And SUM

sql,count,sum,base,libreoffice
I have a table having this table done_by Var_ID| Var_name| Q1_by |Q2_by|Q3_by|Q4_by 1 | abc | me | me |me |you 2 | cba | me | me |you |you 3 | abd | me | you |you |me the result i want to get is get all the total...

## Working with sums in R

r,sum
How does one work with sums in R? I can't seem to find an easy way to calculate sums \sum_{i=m}^n a_i. There are three things to decide here; where summation starts, where it ends, and what elements are to be summed. I have a data frame df and I would...

## Programmatically calc sum of rows in Excel

excel,sum,rows
From the above screenshot, I'm trying to add multiple rows into the F column from cells C and D. I have manually done some of them myself, ex: Cell F2 is =SUM(C2+D2), Cell F3 is =SUM(C3+D3) Is there a way to copy this sum forumla and increment it for...

## Efficient way to sum overlapping date intervals with several groupings in PostgreSQL version 8.4

sql,postgresql,group-by,sum
Hello I'm new to stackoverflow and relatively new to psql so please be lenient if I do things wrong. I have a large data set showing animal movements that looks a little like this: animalid | movementdate | offmovementdate | location | rsk ========================================================== 1 | 1998-01-01 | 1998-04-01 |...

## Java Add Numbers In String

java,arrays,string,sum,substring
I'm creating an application which will take an image as input and use tesseract and give an output. This image has numbers (can vary the numbers) I'm getting an output of, (when i run on the image) 0 1 12 1 0 I'm currently getting 5which is 0+1+1+2+1+0 cause im...

## Python Pandas sum of dataframe with one column

python-2.7,pandas,sum,dataframes,calculated-columns
I have a Python Pandas DataFrame: df = pd.DataFrame(np.random.rand(5,3),columns=list('ABC')) print df A B C 0 0.041761178 0.60439116 0.349372206 1 0.820455992 0.245314299 0.635568504 2 0.517482167 0.7257227 0.982969949 3 0.208934899 0.594973111 0.671030326 4 0.651299752 0.617672419 0.948121305 Question: I would like to add the first column to the whole dataframe. I would like...

## Query to sum the previous values

sql,oracle,sum,running-total
I have the table strucure as in the image. I need to get the values added to the sum of previous values(shown in the REQUIRED RESULT) I tried with the following query SELECT empid, sum(tot_hours) OVER (PARTITION BY empid ORDER BY empid ) AS tot_hours FROM empDet ORDER BY empid...

## how to sum a field and distribute the sum between records in another table

sql,sql-server,select,sum

## VBA SUM Variable Range

excel,vba,variables,sum,range
I want a code to sum the Variable rows up if certain condition is met. e.g. If A12 is numeric and B12 is empty then insert a fomula in cell C12 to sum C3:C11. Then perform the same action at C22 and C30. The problem I have is don't...

## Dynamical SUMIF command

excel,sum,condition
If it is possible, I'd like to create a formula that will allow me the following: Formula must be in the entire column or in this example, in the range B1:B5. The formula is based on a condition, that when the total sum of cells from column A is lower...

## Dsum Function in Query Field not working properly

ms-access,sum,ms-access-2010,ms-access-2013,running-total
I have a date based query that returns two fields, Week Ending Date and L2N, and I want to add a third field that provides a rolling total of the L2N field. I have tried using DSum as follows: RunL2N: DSum("[L2N]","Occupied Apts & L2N", "Week Ending Date=" & "'" &...

## R - passing variable containing column name into sum()

r,variables,sum
Let's say we've attached a data from a table containing column 'banana' with numeric values. colName='banana' Now we want the sum of that column, assuming we don't know what it's called, we only know the name is being stored in the colName variable. How do we call the sum() function?...

## SUBTRACT two columns SUM, then SUBTRACT the result and SUM the result to show minimum quantity

mysql,count,group-by,sum
Is a little complicated the question, but I need to show the SUM of SUBTRACT two columns SUM and then SUBTRACT the result. Right now I have the result (1,1,1) when the minimum quantity is below of "x" value for each product but right now show me three buttons(because there...

## SUM query, between dates, grouped by employee

sql-server-2012,sum,aggregate
I have 4 tables, structured as follows: More verbosely, a project consists of a set of tasks. An employee can add one or more notes to the database, specifying the amount of time spent on that task on any given date. I would like to find for each employee, how...

## Group array by common id and date and sumarize to calendar table

php,arrays,date,sum,group
I've been ripping my hair out on this one. Struggling with understanding how i really want to attack this problem, even the basic logic... I've got this dataset: Array ( [0] => stdClass Object ( [id] => 233773869 [pid] => 9919304 [tid] => 6754304 [uid] => 1502708 [project] => **HIDDENNAME**...