SQLUSA

Microsoft SQL Server 2005

Best Practices

 

How to exclude some rows from a sum?

 

The following script illustrates the exclusion of some rows from the first total while including all rows in the second total:

USE AdventureWorks

SELECT Color=ISNULL(Color,'NOCOLOR'),
StandardCostTotal=SUM(CASE WHEN DaysToManufacture > 2 THEN StandardCost ELSE 0 END),
ListPriceTotal=SUM(ListPrice)
FROM Production.Product
GROUP BY Color
ORDER BY Color

 

 

The World Leader in SQL Server Training
 
SQLUSA.com Home Page