Try below code
select * into #temp1 from
(select 82983500000 UPC,1StoreNumber,3 QtySold union all
select 82983500001,2,1 union all
select 82983500002,3,4 union all
select 82983500003,4,5 union all
select 82983500004,5,6 union all
select 82983500005,6,8 union all
select 82983500006,7,9 union all
select 82983500007,8,3 union all
select 82983500008,9,6 union all
select 82983500009,1,7 union all
select 82983500010,2,8 union all
select 82983500011,3,9 union all
select 82983500012,4,0 union all
select 82983500013,5,3 union all
select 82983500014,6,4 union all
select 82983500015,7,7 union all
select 82983500016,8,8 union all
select 82983500017,9,3 union all
select 82983500018,1,1 union all
select 82983500019,2,4 union all
select 82983500020,3,5 union all
select 82983500021,4,6 union all
select 82983500022,5,8 union all
select 82983500023,6,9 union all
select 82983500024,7,3 union all
select 82983500025,8,6 union all
select 82983500026,9,7 union all
select 82983500027,1,8 union all
select 82983500028,2,9 union all
select 82983500029,3,0 union all
select 82983500030,4,3 union all
select 82983500031,5,4 union all
select 82983500032,6,7 union all
select 82983500033,7,8 union all
select 82983500034,8,3 union all
select 82983500035,9,1 union all
select 82983500036,1,4 union all
select 82983500037,2,5 union all
select 82983500038,3,6 union all
select 82983500039,4,8 union all
select 82983500040,5,9 union all
select 82983500041,6,3 union all
select 82983500042,7,6 union all
select 82983500043,8,7 union all
select 82983500044,9,8 union all
select 82983500045,1,9 union all
select 82983500046,2,0 union all
select 82983500047,3,3 union all
select 82983500048,4,4 union all
select 82983500049,5,7 union all
select 82983500050,6,8 union all
select 82983500051,7,3 union all
select 82983500052,8,1 union all
select 82983500053,9,4 union all
select 82983500054,1,5 union all
select 82983500055,2,6 union all
select 82983500056,3,8 union all
select 82983500057,4,9 union all
select 82983500058,5,3 union all
select 82983500059,6,6 union all
select 82983500060,7,7 union all
select 82983500061,8,8 union all
select 82983500062,9,9 union all
select 82983500063,1,0 union all
select 82983500064,2,3 union all
select 82983500065,3,4 union all
select 82983500066,4,7 union all
select 82983500067,5,8 union all
select 82983500068,6,3 union all
select 82983500069,7,1 union all
select 82983500070,8,4 union all
select 82983500071,9,5 union all
select 82983500072,1,6 union all
select 82983500073,2,8 union all
select 82983500074,3,9 union all
select 82983500075,4,3 union all
select 82983500076,5,6 union all
select 82983500077,6,7 union all
select 82983500078,7,8 union all
select 82983500079,8,9 union all
select 82983500080,9,0 union all
select 82983500081,1,3 union all
select 82983500082,2,4 union all
select 82983500083,3,7 union all
select 82983500084,4,8 union all
select 82983500085,5,3 union all
select 82983500086,6,1 union all
select 82983500087,7,4 union all
select 82983500088,8,5 union all
select 82983500089,9,6 union all
select 82983500090,1,8 ) a
DECLARE @ReportColumnNames AS NVARCHAR(MAX)
select @ReportColumnNames= COALESCE(@ReportColumnNames + ', ', '')+Storename from (select distinct 'Storenumber-'+convert(varchar,Storenumber) as Storename from #temp1) a
print @ReportColumnNames
--select UPC,'Storenumber-'+convert(varchar,Storenumber),Qtysold from #temp1
DECLARE @columns NVARCHAR(MAX),@totalcolumns NVARCHAR(MAX), @sql NVARCHAR(MAX);
SELECT @columns = N'',@totalcolumns=N''
SELECT @columns += N', p.' + QUOTENAME(Storename)
FROM (select distinct 'Storenumber-'+convert(varchar,Storenumber) as Storename from #temp1) AS x;
Select @totalcolumns += N'+ isnull(p.' + QUOTENAME(Storename)+',0)'
FROM (select distinct 'Storenumber-'+convert(varchar,Storenumber) as Storename from #temp1) AS x;
print @totalcolumns
--Type 1 just store number
SET @sql = N'
SELECT ' + STUFF(@columns, 1, 2, '') + '
FROM
(
select ''Storenumber-''+convert(varchar,Storenumber) as Storename,sum(Qtysold) as Qtysold from #temp1
group by ''Storenumber-''+convert(varchar,Storenumber)
) AS j
PIVOT
(
SUM(Qtysold) FOR Storename IN ('
+ STUFF(REPLACE(@columns, ', p.[', ',['), 1, 1, '')
+ ')
) AS p;';
PRINT @sql;
EXEC sp_executesql @sql;
--Type 2 store number with UTC & sale total of all store
SET @sql = N'
SELECT UPC,' + STUFF(@totalcolumns, 1, 2, '') + 'as [sale total of all store],' + STUFF(@columns, 1, 2, '') + '
FROM
(
select UPC,''Storenumber-''+convert(varchar,Storenumber) as Storename, Qtysold from #temp1
) AS j
PIVOT
(
SUM(Qtysold) FOR Storename IN ('
+ STUFF(REPLACE(@columns, ', p.[', ',['), 1, 1, '')
+ ')
) AS p;';
PRINT @sql;
EXEC sp_executesql @sql;
No comments:
Post a Comment