Sum two rows with different IDs

sql sum multiple rows with different id
sql sum multiple rows with same id
sql query to sum two columns values based on it's record
sum(1) sum(2) sum(3) sql
sql sum group by multiple columns
sql sum multiple columns
php mysql sum multiple columns
sql sum row values

I have the following data:

ID | Field A | Quantity
1  |  A   | 10 
2  |  B   | 20
3  |  C   | 30

I would like to sum the fields with ids 1 and 3 in a way that result will be:

ID | Field A | Quantity
1  |  A   | 40 
2  |  B   | 20

Sounds to me more like code manipulation rather than SQL, but still want to try it.

My DMBS is sql-server.

Not sure what method to use to combine 1,3 ID but you could try case:

select 
case when id in (1,3) then 1 else id end
, min("Field A") "Field A"
, sum(quantity) quantity from myTable 
group by case when id in (1,3) then 1 else id end

The above uses group by to aggregate the data. In this case it organizes the ID field using some logic to combine 1,3. All other unique ID will have its own group.

Aggregate functions take care of the other fields, including logic to take the min() value for Field A which seems to fit your requirement

Multiple join and sum query producing wrong output, SELECT META(coffee).id as coffeeId, SUM(cold_coffee.`number`) as `ccNumber` SUM over distinct rows with multiple joins. postgresql, join  To sum cells based on multiple criteria we can use SUMIF or SUMIFS function depending on the condition. If you want to learn SUMIFS function then this article is especially for you. In this article we will focus on how to sum values meeting matches values from multiple rows of data.

you can try by using case when

select case when id in(1,3) then 'A' else 'B' end as field,sum(Quantity) as Quantity
from tablename group by case when id in(1,3) then 'A' else 'B' end

SQL Server SUM() Function By Practical Examples, This tutorial shows you how to use SQL Server SUM() function to calculate the sum of values. DISTINCT instructs the SUM() function to calculate the sum of the only distinct INTO t(val) VALUES(1),(2),(3),(3),(4),(NULL),(5); SELECT val FROM t; (1 row affected) The following statement finds total stocks by store id​:. From this all the sum values of column B, C and D will be found. Download The Working File. How to Sum Multiple Rows and Columns in Excel. Related Readings. How to divide columns in Excel (Top 8 ways) Excel formula to compare two columns and return a value (5 examples) VLOOKUP Formula to Compare Two Columns in Different Sheets!

I think simple aggregation does what you want:

select min(id) as id, min(fieldA) as fieldA, sum(quantity) as quantity
from t
group by (case when id in (1, 3) then 1 else id end);

Sum sql for data in multiple columns and across rows with Total , For a better understanding we will change our student table a bit by adding marks in different subjects for each record. Now the table will be like this. id, name  Select the rows you will combine, and click Kutools > Content > Advanced Combine Rows. See screenshot: 2. In the Advanced Combine Rows dialog box, please select the ID or name column you will combine by, click Primary Key, and then specify combination rules for other columns as you need.

SUM over distinct rows with multiple joins, I understand, that in case of LEFT OUTER JOIN there will be 2 rows for item with items.id = 1 (and so on for other matches), but I don't know how  Sum two rows with different IDs. Ask Question Asked 1 year, 5 months ago. I would like to sum the fields with ids 1 and 3 in a way that result will be: ID | Field

Solved: Summation of values in rows with same ID, Summation of values in rows with same ID Message 2 of 13 is a snippet of the ID numbers, its a concatenation of two other fields and is  Is there a way to merge my Count column's value of two rows that have the same UPC (item ID) but only show the 2nd (latest) info of the rest of the column like this? Because my 'Unit Cost' has two different value, my DISTINCT function still produces two rows instead of just one. If I just use TOP 1, then my Count value will not SUM both qty.

Sum data by using a query - Access, In addition, Access provides several ways to add Sum and other aggregate  Re: Sum all rows with the same ID John Sobczak Jun 6, 2016 3:10 AM ( in response to Carlos Edo ) If you add your date filter to context or if you add date to the fixed expression it should work.

Comments
  • This works but I won't voted as the answer since It wasn't the first suggestion. Thanks anyways :)
  • @GabrielMuñumel Gordon was the last user to post an answer, Zaynul was first, though, that is not technically how this process works. You should select the best (most helpful) post as your accepted answer
  • This works but I won't voted as the answer since It wasn't the first suggestion. Thanks anyways :)