Site sponsored by: Idera Try Idera’s new SQL admin toolset
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 you SQL Server knowledge with others and raise your profile in the community More...
Latest Articles

Capture DDL Changes using Change Data Capture with SQL Server 2008 ...
Business Intelligence in Collaborative Planning, Forecasting and Replenishment
Inside SQL Server Cluster Setup and Troubleshooting Techniques - Part I ...
Configure and Manage Policy Based Management in SQL Server 2008 ...

More     
 
Latest FAQ's

Cannot Start SQL Server Service
Users are able to connect to report manager but not able ...
Errors when SQL Server Snapshot Replication is Running
How to Display Server Name or IP Address in a Reporting ...

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 2 / 3

This query fetches the following result.

Type

Tot_Num

-----

-------

GSM

8

CDMA

8

3G

4

The primary rule for using the GROUP BY clause is to make sure all the columns in your select list are present in your GROUP BY clause.

SELECT Type, Make, COUNT(Type) AS Tot_Num
FROM Mobiles_phones
GROUP BY Type, Make

This query gives you the following result.

Type

Model

Tot_Num

----

-----

-------

GSM

Nokia

3

CDMA

Nokia

2

3G

Nokia

2

Suppose you wanted to list all the mobile phone types that have a minimum of four entries. You might think of using a WHERE clause, but remember that you should not use aggregate functions in the WHERE clause. Instead use the HAVING clause.

SELECT Type, COUNT (Type) AS Tot_Num
FROM Mobiles_phones GROUP BY Type
HAVING COUNT (Type) > 4

This query will give you the following result.

Type

Tot_Num

----

-------

GSM

8

CDMA

8



Essential Aggregate Functions

Aggregate functions are acceptable as expressions in the following:

  • The select list of a SELECT statement.
  • A COMPUTE or COMPUTE BY clause.
  • A HAVING clause.

Transact-SQL has seven key aggregate functions. We will use the table below to explain the basic uses of these functions.

Name

Age

Salary

Alex

34

11000

Bob

23

20000

Cathy

22

12500

Dona

19

35000

James

21

16000

Kelly

45

21550

Mosses

35

20600

Nancy

21

16000

Peter

23

16000

Rosalie

32

10000

Samuel

45

11000

1. MAX (column)

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

Syntax:

SELECT MAX (column) FROM Table

Example:

SELECT MAX (salary) FROM Employee

Result: 35000.

2. MIN (column)

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

Syntax:

SELECT MIN (column) FROM Table

Example:

SELECT MIN (salary) FROM Employee

Result: 11000.


<< Prev Page     Next 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