Home > SQL Server > Compute Sum and Average using aggregation function – SQL Server

Compute Sum and Average using aggregation function – SQL Server


Sum() and Avg() are aggregate functions that are used to compute sum and average of a set of numeric values. These functions ignores the NULL values from the list. We can also compute distinct aggregation using Distinct keyword with in function argument.

Function syntax:

SUM ( [ ALL | DISTINCT ] expression or column name) – This function returns the Sum of all the values.

AVG ( [ ALL | DISTINCT ] expression or column name) – This function returns the Average of all the values.

Implementation

I have created a table and stored some value as shown in the below image.

pic1Example for sum() function

pic2Example for avg() function

pic3

We can also use over clause with these aggregation functions to compute moving averages, cumulative aggregates, running totals, or  top N per group.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: