Using Count with Distinct in SQL Server 2008

In this article I describe how to use Count with distinct keyword in SQL
  • 4124

Introduction

Count is one of the aggregate function in SQL Server 2008 and used to count the number of rows in a database table. Aggregate functions include avg, max, min, count and sum functions. Count can be used with two keyword, one is ALL and another is DISTINCT. Count with DISTINCT keyword is used to return number of unique rows. Count function does not count the null values from the column.

Example:

Return number of distinct rows from table

SELECT [ID]
,[F_Name]
,[L_Name]
,[Salary]
,[Region]
,[City]
FROM [deep].[dbo].[Employee]
GO
select COUNT(distinct f_Name)as TotalEmployee from Employee

Output:

Count(Distinct)Function-in-sql.jpg


Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.