i using sql server 2012. final result @sql temp table.here code far. help. if object_id ('tempdb.dbo.#my_dt_cte') not null drop table #my_dt_cte create table #my_dt_cte ([rowid] int not null identity (1,1) ,[yyyymm] int ) ; my_dt_cte ( select convert(int,convert(varchar(6),eomonth(getdate(),-1),112)) [yyyymm] union select convert(int,convert(varchar(6),eomonth(getdate(),-2),112)) [yyyymm] union select convert(int,convert(varchar(6),eomonth(getdate(),-3),112)) [yyyymm] union select convert(int,convert(varchar(6),eomonth(getdate(),-4),112)) [yyyymm] union select convert(int,convert(varchar(6),eomonth(getdate(),-5),112)) [yyyymm] union select convert(int,convert(varchar(6),eomonth(getdate(),-6),112)) [yyyymm] ) insert #my_dt_cte select [yyyymm] my_dt_cte order [yyyymm] desc; -- select * #my_dt_cte declare @columns nvarchar(max), @sql nvarchar(max); set @columns = n''; select @columns += n', p.' + quotename(yyyymm) (select p.yyyymm #my_dt_cte p group p.yyyymm...