sql-serverstimulsoft

Sum of multiple columns in SQL


I have a report like this

ID     Score 1     Score 2     Sum     Average
1        5            5         10        5
2        7            6         13       6.5
3        4            8         12        6
.        .            .          .        .
.        .            .          .        .
.        .            .          .        .
Total    16           19        35       17.5

I have Score 1 and Score 2, but I need to get Sum, Average, and Total through Stimulsoft.

How can I use sum for getting summation of two columns in each row?


Solution

  • Hope this Sql query helps:

    Select CAST(ID AS VARCHAR(10)) AS [ID], Score1, Score2, (Score1 + Score2) AS [SUM], ((Score1 + Score2)/2) AS [AVG] from Yourtable
    UNION ALL
    Select 'Total', SUM(Score1), SUM(Score2), SUM((Score1 + Score2)), SUM(((Score1 + Score2)/2)) from Yourtable  
    

    You can also look into computed column for sum and average columns if its a frequently used query.