Sometimes you need to do a report with totals column and with totals that depend on some condition, like
Product, Total Sales this year, total sales this month.
Use SUM(Case…) condition, like here:
SELECT SUM(CASE WHEN order_date >= '01/01/09' THEN quantity ELSE 0 END) AS items_sold_since_date, SUM(quantity) AS items_sold_total, product_ID FROM Sales GROUP BY product_ID
Another way to do Sum:
COUNT(distinct case when Status.Complete = 1 then Status.ID else NULL end) as [Count Of Complete Status],