Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Another Cross-Tab Question

Author  Topic 

shazam27
Starting Member

1 Post

Posted - 2005-03-15 : 14:26:12
OK - I went through several cross-tab examples and followed many links, and I did get the first of my two cross tab issues resolved - thanks for that...

Now here is the 2nd problem I am having...

Here is what I want to go from:
IDNum QNum LongResponse
0011111 8 No Response
0011111 9 Probably a ...
0022222 8 No Response
0022222 9 Lack of contract ...
0033333 8 Our company is ...
0033333 9 Blah Blah..

And here is what I am hoping to do:
IDNum QNum8 QNum9
0011111 No Response Probably a ...
0022222 No Response Lack of contract ...
0033333 Our company is ... Blah Blah..

from the examples I have found, this is the code I was trying:
select EditBy,
sum(case QuestionNumber when 8 then LongAnswerText else '' end) QNum8,
sum(case QuestionNumber when 9 then LongAnswerText else '' end) QNum9
from
SURVEY_CrosstabLongAnswerStart
group by editby

But, as you can see, you cant put an nVarChar into a data type int...and TransSQL does not support the aggregate function First like I able to use in Access.

Any suggestions?

Thanks,
Scott

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-03-15 : 14:29:15
see if this sproc helps you

create proc spTranspose
@TableName varchar(100),
@PrimaryCol varchar(100), -- parent column to transposing columns
@SecondaryCol varchar(1000) -- CSV string of columns to transpose
as

select @SecondaryCol = replace(@SecondaryCol, ' ', '')

declare @i int, @tInc varchar(10), @tIncNext varchar(10), @Sql varchar(8000),
@SqlSelect varchar(8000), @tempSql nvarchar(4000), @ColNum int

select @i=1, @Sql = '', @SqlSelect = ''

select @tempSql = 'select top 1 @ColNum = count(*) from ' + @TableName + ' group by ' + @PrimaryCol + ' order by count(*) desc'
exec sp_executesql @tempSql, N'@ColNum int output', @ColNum output
if object_id('tempdb..##temp_transpose') is not null
drop table ##temp_transpose

exec ('select identity (int, 1,1) as Transpose_Id, ' + @PrimaryCol + ', ' + @SecondaryCol +
' into ##temp_transpose from ' + @TableName + ' order by ' + @PrimaryCol + ', ' + @SecondaryCol)

select @SqlSelect = 'select t1.' + @PrimaryCol,
@Sql = ' from ##temp_transpose t1'

while @i<=@ColNum
begin
set @tInc = 't' + cast(@i as varchar(10))
set @SqlSelect = @SqlSelect + ', ' + @tInc + '.'+ replace(@SecondaryCol, ',', ', ' + @tInc + '.')
set @tIncNext = 't' + cast(@i+1 as varchar(10))
if @i<@ColNum
set @Sql = @Sql + ' LEFT JOIN ##temp_transpose ' + @tIncNext + ' on t1.' + @PrimaryCol + ' = ' + @tIncNext + '.' + @PrimaryCol + ' and ' + @tInc + '.Transpose_Id = ' + @tIncNext + '.Transpose_Id-1 '
set @i = @i + 1
end
exec (@SqlSelect + @Sql + ' WHERE (select count(*) from ##temp_transpose where t1.' + @PrimaryCol + ' = ' + @PrimaryCol + ' and t1.Transpose_Id > Transpose_Id)=0 order by t1.' + @PrimaryCol)
go

exec spTranspose 'pubs..authors', 'au_lname', 'au_fname, phone'


Go with the flow & have fun! Else fight the flow
Go to Top of Page
   

- Advertisement -