Your SlideShare is downloading. ×
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With Functions
MS Sql Server: Doing Calculations With Functions
Upcoming SlideShare
Loading in...5
×

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.
Text the download link to your phone
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 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

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

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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 <condition>;
    select * from tablename where colname=any(cond);
    select * from tablename where colname in (condition);
  • 6. Advanced Aggregates
    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.
  • 7. Advanced Aggregates
    Consider an employee table:
    Find the Number of Employees working in each department:
    Select count(empid), depid from employee;
    Result:
  • 8. Additional Functions
    • upper (fieldName)
    Converts the value of fieldName to upper case. Can be used with strings.
    • lower (fieldName)
    Converts the value of fieldName to lowercase. Can be used with strings.
  • 9. Summary
    10. Doing Calculations with functions
  • Visit more self help tutorials
    Pick a tutorial of your choice and browse through it at your own pace.
    The tutorials section is free, self-guiding and will not involve any additional support.
    Visit us at www.dataminingtools.net

×