Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# MS Sql Server: Doing Calculations With Functions

2,624

Published on

Doing Calculations With Functions

Doing Calculations With Functions

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
2,624
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
0
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. 10
SQL SERVER: DOINGCALCULATIONS WITH FUNCTIONS
• 2. Mathematical Functions
Can mathematical functions be used on my tables?
Yes. Microsoft SQL Server 2008 provides several mathematical functions such as sum(col), avg(col), min(col), max(col), count(col) etc. In SQL, they are referred to as aggregate functions as they work upon aggregates of rows.
Functions Explained:
Sum(fieldName): Find the sum of field values of all records
Avg(fieldName): Find the average of field values of all records
Min(fieldName): Find the minimum of the field values of all records
Max(fieldName): Find the maximum of the field values of all records
Count(fieldName): Find the number of field values in the table records
• 3. Mathematical Functions
Consider an Interpol database which contains a table of the biggest robberies that took place this year, all around the world.
Now, lets look into the application of math functions over this table.
• 4. Mathematical Functions
1. Find the TOTAL booty of all the robberies:
Select sum(booty) from robberies;
2. Find the Average booty of all the robberies:
Select avg(booty) from robberies;
3. Find the robbery with the maximal booty
Select max(booty) from robberies;
4. Find the robbery with the minimal booty
Select min(booty) from robberies;
5. Find the number of robbery cases:
Select count(booty) from robberies;
• 5. Using as condition
HEY??? I CANNOT USE THESE FUNCTIONS WITH MY ‘WHERE’ CONDITION???
Microsoft SQL Server 2008 restricts the user to use these functions with ‘WHERE’ conditions. Therefore, to solve our problem three keywords: ‘having’, ‘any’ and ‘in’
select * from tablename having &lt;condition&gt;;
select * from tablename where colname=any(cond);
select * from tablename where colname in (condition);
IS THERE ANY OTHER MODIFICATION TO THESE FUNCTIONS?
We can combine these functions with ‘group by’ function for better results.
select sum(col1),col2 from tablename group by col2;
The above command will find out the sum of each group from column 2
More than one aggregate functions can be used simultaneously, seperated by commas.
Eg: select sum(col1), count(col1) from tablename;
Consider the example in the next slide.
Consider an employee table:
Find the Number of Employees working in each department:
Select count(empid), depid from employee;
Result: