> select Vend.Name, SUM(TotalDue) as VendorTotal > from Purchasing.Vendor as Vend > left outer join Purchasing.PurchaseOrderHeader AS POH > on Vend.BusinessEntityID = POH.VendorID > where OrderDate BETWEEN '1-1-2008' AND '12-31-2008' -- want just order dollars for 2008 > group by Vend.Name > order by VendorTotal desc >>The results only show 86 vendors (just the vendors who had orders in 2008). The numbers are correct for those 86 vendors, but we don't see the 18 vendors who didn't have orders.
>...where OrderDate BETWEEN '1-1-2008' AND '12-31-2008' OR OrderDate IS NULL >>
> ;WITH POTemp as > ( SELECT VendorID, SUM(TotalDue) as VendorTotal > FROM Purchasing.PurchaseOrderHeader > WHERE where OrderDate BETWEEN '1-1-2008' AND '12-31-2008' > GROUP BY VendorID) > > select Vend.Name, VendorTotal > from Purchasing.Vendor as Vend > left outer join POTemp > on Vend.BusinessEntityID = POTemp.VendorID > order by VendorTotal desc >There is no need for a CTE. IMO, the single best approach is to include the where clause into the join itself
select Vend.Name, SUM(TotalDue) as VendorTotal from Purchasing.Vendor as Vend left outer join Purchasing.PurchaseOrderHeader AS POH on Vend.BusinessEntityID = POH.VendorID AND POH.OrderDate BETWEEN '1-1-2008' AND '12-31-2008' group by Vend.Name order by VendorTotal descBTW as a good practise, in SQL you format your dates in ANSI format ('yyyymmdd') as the american format scares the hell out of non-us programmers.