Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

Dynamic Crosstab with multiple PIVOT Columns

Aug 27 2007 9:30AM by Madhivanan   

Jeff Smith in his weblog showed how to generate Crosstab reports using a stored procedure. It works only for one PIVOT Column.I had a requirement to generate crosstab reports with more than one pivot column. So I used the same approach he used and modified his procedure as shown below

CREATE procedure CrossTab 
(
@select varchar(2000),
@PivotCol varchar(100), 
@Summaries varchar(100), 
@GroupBy varchar(100),
@OtherCols varchar(100) = Null
)

AS
set nocount on
set ansi_warnings off 
declare @sql varchar(8000)

Select @sql = ''

Select @OtherCols= isNull(', ' + @OtherCols,'')

create table #pivot_columns (pivot_column_name varchar(100))

Select @sql='select ''' + replace( + @PivotCol,',',''' as pivot_column_name union all select ''')+''''

insert into #pivot_columns
exec(@sql)

select @sql=''

create table #pivot_columns_data (pivot_column_name varchar(100),pivot_column_data varchar(100))

Select @PivotCol=''

Select @PivotCol=min(pivot_column_name) from #pivot_columns

While @PivotCol>''
Begin
    insert into #pivot_columns_data(pivot_column_name,pivot_column_data) 
    exec 
    (
    'select distinct ''' + @PivotCol +''' as pivot_column_name, convert(varchar(100),' + @PivotCol + ') as pivot_column_data    from 
    ('+
        @select
    +'
    ) T'
    )

    Select @PivotCol=min(pivot_column_name) from #pivot_columns where pivot_column_name>@PivotCol
end 
select 
    @sql = @sql + ', ' + 
    replace(
        replace(
                @Summaries,'(','(CASE WHEN ' + Pivot_Column_name + '=''' + 
                pivot_column_data + ''' THEN ' 
                    ),
            ')[', ' END) as [' + pivot_column_data 
                )
from #pivot_columns_data
order by pivot_column_name

exec 
(
     'select ' + @GroupBy +@OtherCols +@sql + 
    ' from (
    '+
        @select 
    +'
    ) T
    GROUP BY ' + @GroupBy
) 
drop table #pivot_columns
drop table #pivot_columns_data

set nocount off
set ansi_warnings on

Now execute the procedure by supplying two pivot columns shipcountry and Year(Orderdate)

EXEC CrossTab
'SELECT LastName, OrderDate,shipcountry FROM northwind..Employees Employees 
INNER JOIN northwind..Orders Orders ON (Employees.EmployeeID=Orders.EmployeeID) ',
'shipcountry,Year(OrderDate)',
'Count(LastName)[]',
'LastName'

Tags: t-sql, sql_server, dynamic_cross_tab, dynamic_pivot, cross_tab, pivot,


Madhivanan
3 · 40% · 12958
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Dynamic Crosstab with multiple PIVOT Columns" rated 5 out of 5 by 2 readers
Dynamic Crosstab with multiple PIVOT Columns , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]