datetime century date into pad dynamic cursor money percent sp job isnumeric isdate over update
SQLUSA.com
SQL 2008 GRAND SLAM ON 49 CD
FREE TRIAL  CLICK HERE TO ORDER  SEARCH
SQL Server Training SQL 2005 Scripts SQL 2008 Articles
SQL JOBS News Format Developer
How to perform the equivalent of GROUP BY?

Execute the following T-SQL example scripts in Microsoft SQL Server Management Studio Query Editor to find maximum list price for each product subcategory without and with GROUP BY.

-- SQL category processing without using GROUP BY
USE AdventureWorks;
SELECT   DISTINCT SubcategoryName = d.Name,
                  MaxListPrice = ListPrice
FROM     Production.ProductSubcategory d
         JOIN Production.Product p
           ON d.ProductSubcategoryID = p.ProductSubcategoryID
WHERE    ListPrice > 0.0
AND convert(VARCHAR,p.ProductSubcategoryID) + convert(VARCHAR,ListPrice)
NOT IN (SELECT convert(VARCHAR,p1.ProductSubcategoryID) +
               convert(VARCHAR,p1.ListPrice)
-- SQL self-join
        FROM   Production.Product p1
                INNER JOIN Production.Product p2
           ON p1.ProductSubcategoryID = p2.ProductSubcategoryID
            AND p2.ListPrice > p1.ListPrice)
ORDER BY 1;

GO

SubcategoryName MaxListPrice
Bib-Shorts 89.99
Bike Racks 120
Bike Stands 159
Bottles and Cages 9.99
Bottom Brackets 121.49
Brakes 106.5
Caps 8.99
Chains 20.24
Cleaners 7.95
Cranksets 404.99
Derailleurs 121.46
Fenders 21.98
Forks 229.49
Gloves 37.99
Handlebars 120.27
Headsets 124.73
Helmets 34.99
Hydration Packs 54.99
Jerseys 53.99
Lights 44.99
Locks 25
Mountain Bikes 3399.99
Mountain Frames 1364.5
Panniers 125
Pedals 80.99
Pumps 24.99
Road Bikes 3578.27
Road Frames 1431.5
Saddles 52.64
Shorts 69.99
Socks 9.5
Tights 74.99
Tires and Tubes 35
Touring Bikes 2384.07
Touring Frames 1003.91
Vests 63.5
Wheels 357.06

 
-- Equivalent GROUP BY statement
SELECT   DISTINCT SubcategoryName = d.Name,
                  MaxListPrice = max(ListPrice)
FROM     Production.ProductSubcategory d
         INNER JOIN Production.Product p
           ON d.ProductSubcategoryID = p.ProductSubcategoryID
WHERE    ListPrice > 0.0
GROUP BY d.Name
ORDER BY SubcategoryName

GO

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

Related Articles:

Self Join In Sql Server 2000 2005

http://sqlusa.com/bestpractices2005/selfjoin/


 

Order SQL 2008 GRAND SLAM Today!
SQLUSA.com Home Page
SQL Server Training at www.sqlusa.com.
Microsoft SQL Server 2012 Training Videos at www.sqlusa.com.
SQL Server 2008 Video Training at www.sqlusa.com.
SQL Server 2005 Training Videos at www.sqlusa.com.
Accounting
Administrative
Advertising
Arts
Architecture
Banking
Business Intelligence
Career Jobs
Celebrity
Computer
Consulting
Customer Service
Education
Engineering
Entertainment
Entry Level
Executive
Federal
Finance
Government
Hardware
Healthcare
Hospital
Human Resources
Information Technology
Insurance
Internet
Job Openings
Laboratory
Law Enforcement
Legal
Logistics
Manufacturing
Marketing
Medical
Military
Nursing
Pharmaceutical
Physician
Public Relations
Publishing
Real Estate
Restaurant
Retail
Sales
Social Media
Software
SQL Database
Telecomm
Therapist
Training
Transportation
Truck Driver
Travel
Web
Work from Home

FREE SS SQL / BI OLAP Short Videos on YOUTUBE.com

Microsoft Community Contributor 2011 Microsoft Community Contributor 2012

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

JOIN US ON TWITTER

Copyright 2005-2012, 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.