## Getting sum of 2 columns each with unique condition

excel sum unique values with criteria
how to sum values based on criteria in another column in excel
how to sum cells when value changes in another column?
sum unique values excel
index match sum multiple columns
sum if cell contains number
sumif multiple columns
excel sum range based on cell value

I've two tables like this.

```create table teams (
"ID" Integer NOT NULL ,
"STADIUM_ID" Integer NOT NULL ,
"NAME" Varchar2 (50) NOT NULL ,
primary key ("ID")
) ;

create table matches (
"ID" Integer NOT NULL ,
"WINNER_ID" Integer NOT NULL ,
"OPPONENT_ID" Integer NOT NULL ,
"WINNERSCORE" Integer,
"OPPONENTSCORE" Integer,
primary key ("ID","WINNER_ID","OPPONENT_ID")
) ;
```

They have the following data :

```select * from matches;

ID      WINNER_ID OPPONENT_ID WINNERSCORE OPPONENTSCORE
---------- ---------- ----------- ----------- -------------
1          5           2           5             2
2          4           5           1             0
3          3           2           1             0
4          3           2           1             0
5          1           2           2             0
6          3           1           2             1

select * from teams;

ID     STADIUM_ID    NAME
---------- ---------- -----------
1          1        Team1
2          3        Team2
3          4        Team3
4          2        Team4
5          5        Team5
```

I need to get the sum of the goals for each team. For this aim, tried the following query and got the results below :

```select name,
(select sum(opponentscore)
from matches
where opponent_id = teams.id) +
(select sum(winnerscore) from matches where winner_id = teams.id) sum
from teams;

NAME                                                      SUM
-------------------------------------------------- ----------
Team1                                                       3
Team2
Team3
Team4
Team5                                                       5
```

Do you have any suggestion ?

All you need is to calculate seperately opponentscore and winnerscore by each individual team, and combine them with UNION ALL :

```select name, sum(score) total_score
from
(
select name, sum(winnerscore) score
from teams t join matches m on ( t.id = m.winner_id  )
group by name
union all
select name, sum(opponentscore) score
from teams t join matches m on ( t.id = m.opponent_id  )
group by name
)
group by name
order by 1;
```

SQL Fiddle Demo

How to Use SUMIF with Multiple Criteria in Excel, How do I count unique values in Excel with two conditions? Let's say that you need to sum values with more than one condition, such as the sum of product sales in a specific region. This is a good case for using the SUMIFS function in a formula. Have a look at this example in which we have two conditions: we want the sum of Meat sales (from column C) in the South region (from column A).

you should use join and group by

```  select name, sum(matches.opponentscore) + sum(matches.winnerscore) my_sum
from matches
inner join teams on teams.id = matches.winner_id
group by  teams.name
```

How to sumif with multiple criteria in one column?, 2. Then, press Ctrl + Shift + Enter keys together to get the first total result, and then, Tips: If you need to sum all the unique values based on two criteria, please Sum Multiple Columns Based On Single Criteria In Excel; In Excel, you may  The array formula in cell D3 extracts unique distinct values sorted A to Z, from column B to column D. […] Extract a unique distinct list and sum amounts based on a condition Anura asks: Is it possible to extend this by matching items that meet a criteria?

You could join table teams twice with table matches:

```SELECT name, SUM(wonMatches.WINNERSCORE + lostMatches.OPPONENTSCORE) as goals FROM (teams INNER JOIN matches as wonMatches ON teams.ID = wonMatches.WINNER_ID) INNER JOIN matches as lostMatches ON teams.ID = lostMatches.OPPONENT_ID GROUP BY name```

How to sum unique values based on criteria in Excel?, Sum values based on one or more conditions by using the SUMIFS function in a Let's look more closely at each part of the formula. you want the formula to sum numbers in that column if they meet the conditions. Get instant Excel help. So basically I want to sum up the 1 in each column per Member_Id Browse other questions tagged sql oracle sum unique multiple-columns or ask What conditions

Sum values based on multiple conditions - Excel, Step 1 - Count values in column B. The COUNTIF function counts values based on a condition, I am using multiple conditions, in this case. How to sum multiple columns based on single criteria in Excel? In Excel, you may always need to sum multiple columns based on one criteria. For example, I have a range of data as following screenshot shown, now, I want to get the total values of KTE in three months - Jan, Feb and Mar.

The problem is with `NULL`s - the subqueries return `NULL` when no result is found, and `NULL + anything == NULL`.

Most straightforward fix is:

```select name,
nvl(
(select sum(opponentscore) from matches where opponent_id = teams.id),
0
)
+
nvl(
(select sum(winnerscore) from matches where winner_id = teams.id),
0
) sum
from teams;
```

For performance reasons tohugh, you might want to consider using a joined query with GROUP BY as suggested by others.

Sum number based on corresponding unique value, The criteria is simply that I need to discard any data values for any duplicates, because those values have already been counted for that unique  Using the SUM/SUMPRODUCT Function for Multiple Columns Instead of using the AutoSum feature you can also use the SUM function directly to calculate the total sales for a month. For this type =SUM(B2:B9). Now after pressing Enter,  drag this formulated cell in cell C10 and D10 to calculate the total sales for the month of February and March.

Count unique distinct values in two columns, DISTINCT, Return the SUM of unique values. To get the total SUM of '​advance_amount' of the 'orders' table, the following SQL SUM() using multiple columns example function to sum one or more columns against one or more conditions. To get data of 'cust_country',SUM of 'opening_amt' for each  However, when we use the DISTINCT modifier, the SUM() function returns the sum of only unique values in the val column: SELECT SUM (DISTINCT val) total FROM t; Here is the output: total ----- 15 Warning: Null value is eliminated by an aggregate or other SET operation. (1 row affected)SQL Server SUM() function examples. Let’s take some practical examples of using the SUM() function. A) Simple SQL Server SUM() function example

How to sum values of column B based on UNIQUE values of column , You get instructions on how to install the free tools and sample database. It is typically used in conjunction with aggregate functions such as SUM or Count to summarize values. In SQL groups are unique combinations of fields. Rather than returning every row in a table, when values are grouped, only the unique  If you don't want to rely on the Null behavior, you could always just have the case statement return 1 or 0 and do a sum instead of a count. – JohnFx Jun 21 '10 at 18:38 @JohnFx: Yes, this works, too.

SQL SUM() function, Sum of Unique Count of 1 value in 1 Column based on condition on 2nd Column Location(s) is the one having variable values even within each Group. needs to get the "Unique Count" of groups that has 1s, which is 2. Sum column with a condition in R [duplicate] Ask Question Asked 3 years, 1 month ago. I need to get the sum of column 'b' for each 'a': A B 1 111 2 2 222 3 3 333 1

##### Comments
• The OP sums `opponentscore` where the team is "opponent" and `winnerscore` where the team is "winner". Your query counts both on the same condition.
• Are u sure the syntax is correct? I'm getting missing keyword error
• guess it's something after the second oppening bracket, but i cant figure out how to fix this
• Might need an alias for the derived table (just before inner join)?