Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
USE --Your database here
if exists(select routine_name from information_schema.routines
where routine_name = 'gensp_CrossTab2')
DROP PROCEDURE gensp_CrossTab2
GO
create procedure dbo.gensp_CrossTab2
@SQL varchar(1000),
@PivotCol varchar(100),
@Summaries varchar(100),
@GroupBy varchar(100),
@OtherFields varchar(100) = Null
AS
set nocount on
declare @Vals varchar(5000);
set @Vals = '';
set @OtherFields = isNull(', ' + @OtherFields ,'')
exec ('SELECT Distinct convert(varchar(100),' + @PivotCol + ') as Pivot INTO ##Temp FROM (' +
@SQL + ') A')
select @Vals = @Vals + ', ' +
replace(replace(@Summaries,'(','(CASE WHEN ' + @PivotCol + '=''' + Pivot +
''' THEN '),')[', ' END) as [' + Pivot )
from ##Temp order by Pivot
drop table ##Temp
set nocount off
print 'select ' + @GroupBy + @OtherFields + @Vals + ' from (' + @SQL + ') A GROUP BY ' + @GroupBy
--uncomment the next line to run the statement
--exec ( 'select ' + @GroupBy + @OtherFields + @Vals + ' from (' + @SQL + ') A GROUP BY ' + @GroupBy)