Site sponsored by: Idera The gold standard of SQL Server performance monitoring & diagnostics.
SQL Server Performance

  • Home
  • Articles
  • Forums
  • Tips
  • Quiz
  • FAQ's
  • Blogs
  • Software
  • Books
  • About Us
RSS Feeds
Sign in | Join


Article Topics

All Articles
Performance Tuning
Audit
Business Intelligence
Clustering
Reporting Services
Developer
General DBA
ASP.NET / ADO.NET

Write for Us

Share your SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

System Data Collection Reports
Recover Data Using Database Snapshots
Analyze and Fix Index Fragmentation in SQL Server 2008
Powerful Geographical Visualisations made easy with SQL 2008 Spatial (Part 2) ...

More     
 
Latest FAQ's

How to alter a User Defined Data Type?
How to unzip a File in SSIS?
How to view previous query plans?
ALTER TABLE SWITCH statement failed because the object '%.*ls' is not ...

More     
   
Latest Software Reviews

Spotlight on ApexSQL Doc 2008
ApexSQL Enforce
Embarcadero Change Manager
SQL Server DBA Dashboard

More     

articles >> performance tuning >> Boost Performance and Reduce Code Use With ...

Boost Performance and Reduce Code Use With SQL Server Aggregate Functions

By : Brad McGehee
Feb 03, 2006

Page 3 / 3

3. AVG (column)

The AVG function returns the average value of a selected column. It does not include NULL values in the calculation.

Syntax:

SELECT AVG (column) FROM Table

Example:

SELECT AVG (salary) FROM Employee

This example returns the average age for persons older than 30.

Result: 16240.90.

4. COUNT (Column)

The COUNT function returns the number of not NULL items in a group.

Syntax:

SELECT COUNT (column) FROM Table

Example:

SELECT COUNT (Age) FROM Employee Where salary > 15000

This example finds the number of persons with a value in the "Age" field in the "Persons" table.

Result: 7.

5. SUM (column)

The SUM function returns total sum of a selected column. It does not include NULL values in the calculation.

Syntax:

SELECT SUM (column) FROM Table

Example:

SELECT SUM (Salary) FROM Employee

This example returns the sum of all ages in the "person" table.

Result: 178650.

6. FIRST (column)

The FIRST function returns the value of the first record in the selected field.

Syntax:

SELECT FIRST (column) AS [expression] FROM table

Example:

SELECT FIRST (Age) AS lowest age
FROM Employee
ORDER BY Age

Result: 19.

7. LAST (column)

The LAST function returns the value of the last record in the selected field.

Syntax:

SELECT FIRST (column) AS [expression] FROM table

Example:

SELECT LAST (Age) AS highest age
FROM Employee
ORDER BY Age

Result: 45.

These functions are vital, as they reduce the number of lines in your code and enable you to perform complex SQL queries with ease. These functions operate on an entire group, table, or row, or a set, and significantly increase the scope of the language in terms of data manipulation, and by serving disparate user needs.



Join a discussion about this article in our forum.


<< Prev Page         








Home | Peformance Articles | Audit Articles | Business Intelligence Articles | Clustering Articles | Developer Articles | Reporting Services Articles | DBA Articles | ASP.NET / ADO.NET Articles | DBA FAQ's | Developer Peformance FAQ's | DBA Peformance FAQ's | Developer FAQ's | Clustering FAQ's | Error Messages | Audit Tool Reviews | Backup Tool Reviews | Coding Tool Reviews | Compare Tool Reviews | Documentation Tool Reviews | Design Tool Reviews | Monitoring Tool Reviews | Log Tool Reviews | Reporting Tool Reviews | Clustering Tool Reviews | Security Tool Reviews | Change Management Tool Reviews | Remote Access Tool Reviews | Book Reviews | Security Tool Reviews | QDPMA Performance Tuning | ADO.NET / ASP.NET | Administration | Analysis/OLAP Services | Application Development | Configuration | Components | ETL | Hardware | High Availability | Hints | Index | Misc | Operating Systems | Performance Tuning | Replication | T-SQL | Views


              © 1999-2008 by T10 Media. All rights reserved