SQLUSA.com
SQL SERVER 2008 GRAND SLAM
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL JOBS SQL Server Training Scripts JOB BANK
  SQL Server 2008 Training Scripts  
  SQL Server 2005 Training Scripts  
News SQL Server Articles SQL Format

How to use DISTINCT in SELECT?

Execute the following Microsoft SQL Server T-SQL database scripts in Management Studio Query Editor to demonstrate the use of the DISTINCT keyword. DISTINCT is a very expensive operation.

-- Using DISTINCT in COUNT - QUICK SYNTAX

SELECT TotalRows=COUNT(*), ProductColors=COUNT(DISTINCT Color),

       ProductSizes=COUNT(DISTINCT Size)

FROM AdventureWorks2008.Production.Product

/* TotalRows      ProductColors     ProductSizes

      504         9                 18                */
------------

-- Applying SQL Server DISTINCT to eliminate duplicates in table or result set

SELECT DISTINCT Size, Color

INTO ProdSizeColorCombination

FROM AdventureWorks2008.Production.Product

-- (68 row(s) affected)

------------

-- SQL DISTINCT usage for colors - SQL Server distinct - Select distinct
SELECT DISTINCT ProductColors = COALESCE(Color, 'N/A')
FROM AdventureWorks2008.Production.Product
ORDER BY ProductColors
/*
ProductColors
Black
Blue
Grey
Multi
N/A
Red
Silver
Silver/Black
White
Yellow */
------------
 
-- SQL DISTINCT for order years - SQL Server select distinct on one column
SELECT DISTINCT [Year]=YEAR(OrderDate)
FROM AdventureWorks2008.Sales.SalesOrderHeader
ORDER BY [Year]
GO
/*
Year
2001
2002
2003
2004 */
------------

-- T-SQL COUNT DISTINCT usage - sql count distinct / unique rows
SELECT

      AllProducts=                        COUNT(*),

      ProductsWithSubcategories =         COUNT(ProductSubcategoryID),

      UniqueSubcategories =               COUNT(DISTINCT ProductSubcategoryID),

      ProductsWithColor =                 COUNT(ALL Color),

      UniqueColors =                      COUNT(DISTINCT Color)

FROM AdventureWorks2008.Production.Product

/*

AllProducts ProductsWithSub. UniqueSubcategories ProductsWithColor UniqueColors

504         295               37                  256                  9 */

------------

------------

-- T-SQL SELECT DISTINCT complex usage - create table with distinct ProductID-s

------------

SELECT RowID=ROW_NUMBER() OVER (ORDER BY ProductID),          -- SELECT FROM SELECT

       ProductID

INTO ProdInv                                                  -- Table create

FROM (SELECT DISTINCT ProductID

      FROM AdventureWorks2008.Production.ProductInventory ) x  -- Derived table

-- (425 row(s) affected)

SELECT TOP (7) * FROM ProdInv

/* RowID    ProductID

      1           1

      2           2

      3           3

      4           4

      5           316

      6           317

      7           318   */

------------

 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Microsoft SQL Server 2000 Training Videos at www.sqlusa.com.

FREE SQL & Business Intelligence / OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011
Invest in Your SUCCESS!

Search SQLUSA FREE SQL Server Articles & FREE T-SQL Scripts


Copyright 2005-2011, SMI Corp. All Rights Reserved.

SQL Server 2012 is a program product of Microsoft Corporation.
SQL Server 2008 is a program product of Microsoft Corporation.
SQL Server 2005 is a program product of Microsoft Corporation.
SQL Server 2000 is a program product of Microsoft Corporation.