SQL- Add up Values in a Column

后端 未结 3 1794
旧时难觅i
旧时难觅i 2020-12-31 12:27

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\".

相关标签:
3条回答
  • 2020-12-31 13:16

    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

    • MySQL Reference for SUM()
    0 讨论(0)
  • 2020-12-31 13:17

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

    SELECT    YourRecordID,
              SUM(Gross) AS GrossSum
    FROM      YourTable
    GROUP BY  YourRecordID
    
    0 讨论(0)
  • 2020-12-31 13:21
    select sum(yourCol) as Gross
    from YourTable
    

    Use the aggregate function SUM().

    0 讨论(0)
提交回复
热议问题