Someone correct me if I'm wrong, but doesn't using a view to do the pivot then just selecting from the view in the stored proc make the query execute faster? maybe a better answer for you:create view ProductsPivot as select ProductName, year(DateIn) as Year sum(case Month(DateIn) = 1 then Qty else 0 end) as Jan, sum(case Month(DateIn) = 2 then Qty else 0 end) as Feb, sum(case Month(DateIn) = 3 then Qty else 0 end) as Mar, sum(case Month(DateIn) = 4 then Qty else 0 end) as Apr, sum(case Month(DateIn) = 5 then Qty else 0 end) as May, sum(case Month(DateIn) = 6 then Qty else 0 end) as Jun, sum(case Month(DateIn) = 7 then Qty else 0 end) as Jul, sum(case Month(DateIn) = 8 then Qty else 0 end) as Aug, sum(case Month(DateIn) = 9 then Qty else 0 end) as Sep, sum(case Month(DateIn) = 10 then Qty else 0 end) as Oct, sum(case Month(DateIn) = 11 then Qty else 0 end) as Nov, sum(case Month(DateIn) = 12 then Qty else 0 end) as Dec from Products group by ProductName, year(DateIn)gocreate procedure ProductsByYear @year smallint as set nocount on select ProductName, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec from ProductsPivot where Year = @year order by ProductNamego
Edited by - onamuji on 04/03/2002 11:39:22