There no need for dynamic SQL just use a simple pivot (see
Simple Way To Use Pivot In SQL Query[
^] ) e.g.
select * from
(
select id, revenue, RevenueMonth
from department
) src
PIVOT
(
sum(revenue) for RevenueMonth in (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)
) pvt
Edit after OP comment to solution 2:
Quote:
It's printing till march I want till December.
Is it possible to output till Dec?
Try this:
select isnull(Jan,0) as Jan, isnull(Feb,0) as Feb, isnull(Mar,0) as Mar, isnull(Apr,0) as Apr, isnull(May,0) as May, isnull(Jun,0) as Jun, isnull(Jul,0) as Jul, isnull(Aug,0) as Aug, isnull(Sep,0) as Sep, isnull(Oct,0) as Oct, isnull(Nov,0) as Nov, isnull(Dec,0) as Dec
from
(
select id, revenue, RevenueMonth
from department
) src
PIVOT
(
sum(revenue) for RevenueMonth in (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)
) pvt