MS SQL SERVER: Doing Calculations With Functions

887 views

Published on

MS SQL SERVER: Doing Calculations With Functions

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
887
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MS SQL SERVER: Doing Calculations With Functions

  1. 1. 10<br />SQL SERVER: DOINGCALCULATIONS WITH FUNCTIONS<br />
  2. 2. Mathematical Functions<br />Can mathematical functions be used on my tables?<br /> 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.<br />Functions Explained:<br />Sum(fieldName): Find the sum of field values of all records<br />Avg(fieldName): Find the average of field values of all records<br />Min(fieldName): Find the minimum of the field values of all records<br />Max(fieldName): Find the maximum of the field values of all records<br />Count(fieldName): Find the number of field values in the table records<br />
  3. 3. Mathematical Functions<br />Consider an Interpol database which contains a table of the biggest robberies that took place this year, all around the world.<br />Now, lets look into the application of math functions over this table.<br />
  4. 4. Mathematical Functions<br />1. Find the TOTAL booty of all the robberies:<br />Select sum(booty) from robberies;<br />2. Find the Average booty of all the robberies:<br />Select avg(booty) from robberies;<br />3. Find the robbery with the maximal booty<br />Select max(booty) from robberies;<br />4. Find the robbery with the minimal booty<br />Select min(booty) from robberies;<br />5. Find the number of robbery cases:<br />Select count(booty) from robberies;<br />
  5. 5. Using as condition<br />HEY??? I CANNOT USE THESE FUNCTIONS WITH MY ‘WHERE’ CONDITION???<br />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’<br />select * from tablename having &lt;condition&gt;;<br />select * from tablename where colname=any(cond);<br />select * from tablename where colname in (condition);<br />
  6. 6. Advanced Aggregates<br />IS THERE ANY OTHER MODIFICATION TO THESE FUNCTIONS?<br /> We can combine these functions with ‘group by’ function for better results.<br />select sum(col1),col2 from tablename group by col2;<br />The above command will find out the sum of each group from column 2 <br />More than one aggregate functions can be used simultaneously, seperated by commas.<br />Eg: select sum(col1), count(col1) from tablename;<br />Consider the example in the next slide.<br />
  7. 7. Advanced Aggregates<br />Consider an employee table:<br />Find the Number of Employees working in each department:<br />Select count(empid), depid from employee;<br />Result:<br />
  8. 8. Additional Functions<br /><ul><li> upper (fieldName)</li></ul>Converts the value of fieldName to upper case. Can be used with strings.<br /><ul><li> lower (fieldName)</li></ul>Converts the value of fieldName to lowercase. Can be used with strings.<br />
  9. 9. Summary<br />10. Doing Calculations with functions<br /><ul><li> Sum
  10. 10. Avg
  11. 11. Count
  12. 12. Min
  13. 13. Max
  14. 14. Count
  15. 15. Advanced grouping</li></li></ul><li>Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at www.dataminingtools.net<br />

×