Thursday, April 25, 2013

SQL Server SUM function

SQL Server SUM function

SQL Server 2012 Sums numeric columns and ignores null values.

Price     Item
10.11     shoes
11.22      shirts
NULL    Trousers
33.33     Shorts


select SUM(price)  ->  gives 54.66

>>select total salary of the Employees

select sum(sal) "Total Salary" from emp

 Total Salary
24925.00

>>Select Total Salary of the Employees by Department

select deptno,sum(sal) "Total Salary by Department" from emp
group by deptno

deptno    "Total Salary by Department"
10    8750.00
20    6775.00
30    9400.00

>Get Total Salary and Commission department wise.

select deptno,sum(sal) "Salary",
                      sum(comm) "Comm",
                      sum(sal)+sum(comm) "Sal+comm" from emp
group by deptno

deptno    Salary    Comm    Sal+comm
10    8750.00    100.00    8850.00
20    6775.00    300.00    7075.00
30    9400.00    2200.00    11600.00

Tags: Using SQL Server SUm function, Using SUM aggregate function, How to use SQL Server aggregate function SUM, SQL Server Aggregate functions

No comments:

Post a Comment