10

How can I add up values in a SQL column? I have the table set up in xampp and I'm trying to add up all the values in a column titled "gross".

2
  • Thanks for the quick responses! I've got it working and now have a follow-up question. Is there a way to add up only positive numbers in the column and then add up only negative numbers in the column. Thanks Commented Feb 17, 2012 at 20:15
  • Updated my answer accordingly Commented Feb 17, 2012 at 20:19

3 Answers 3

14

SQL Server or MySQL:

select sum(MyColumn) as MyColumnSum from MyTable 

If you need to sum a column by a grouping of another column

select sum(MyColumn) as MyColumnSum, OtherColumn from MyTable Group By OtherColumn 

Here is a way to, separately, add up negative or positive numbers

select sum( case when MyColumn < 0 then MyColumn else 0 end ) as NegativeSum, sum( case when MyColumn > 0 then MyColumn else 0 end ) as PositiveSum from MyTable 

Reference

Sign up to request clarification or add additional context in comments.

1 Comment

Since he mentions xampp, it's MySQL, but the query should work.
1
select sum(yourCol) as Gross from YourTable 

Use the aggregate function SUM().

Comments

1

Take a look at the SUM() function documentation for MySQL.

SELECT YourRecordID, SUM(Gross) AS GrossSum FROM YourTable GROUP BY YourRecordID 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.