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
 SQL Server Development (2000)
 cross tab...

Author  Topic 

kwikwisi
Constraint Violating Yak Guru

283 Posts

Posted - 2008-06-02 : 22:06:16
this is storedprocedure for cross tab,i also want to add CustomerID and total of gifts base on gift type. how can i do?
Thanks..

PROCEDURE dbo.trymatrix AS
DECLARE @SQLCommand varchar(4096), @column sysname

SET @SQLCommand = 'select Name, ' + char(13)

DECLARE curGift CURSOR STATIC LOCAL FOR

select distinct GiftTitle from Redemption1

OPEN curGift

WHILE 1 = 1

BEGIN

FETCH curGift INTO @column

IF @@FETCH_STATUS <> 0

BREAK

SET @SQLCommand = @SQLCommand +

'sum(case GiftTitle when ''' +@column +''' then 1 else 0 end) as '+ @column + ','+char(13)

END

DEALLOCATE curGift

SET @SQLCommand = left (@SQLcommand, len(@SQLcommand)-2) + char(13)

SET @SQLCommand = @SQLCommand +

'from Customer c

inner join Redemption1 d

on c.CustomerID=d.CustomerID

group by Name'

PRINT @SQLCommand

EXEC (@SQLCommand)

ranganath
Posting Yak Master

209 Posts

Posted - 2008-06-03 : 00:56:35
Hi,

please post your sample data

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-06-03 : 00:57:32
quote:
Originally posted by kwikwisi

this is storedprocedure for cross tab,i also want to add CustomerID and total of gifts base on gift type. how can i do?
Thanks..

PROCEDURE dbo.trymatrix AS
DECLARE @SQLCommand varchar(4096), @column sysname

SET @SQLCommand = 'select Name,CustomerID ' + char(13)

DECLARE curGift CURSOR STATIC LOCAL FOR

select distinct GiftTitle from Redemption1

OPEN curGift

WHILE 1 = 1

BEGIN

FETCH curGift INTO @column

IF @@FETCH_STATUS <> 0

BREAK

SET @SQLCommand = @SQLCommand +

'sum(case GiftTitle when ''' +@column +''' then 1 else 0 end) as '+ @column + ','+char(13)

END

DEALLOCATE curGift

SET @SQLCommand = left (@SQLcommand, len(@SQLcommand)-2) + char(13)

SET @SQLCommand = @SQLCommand +

'from Customer c

inner join Redemption1 d

on c.CustomerID=d.CustomerID

group by Name,CustomerID'

PRINT @SQLCommand

EXEC (@SQLCommand)


The above will give you CustomerID too
Is Gift type another field in your table?
Go to Top of Page

kwikwisi
Constraint Violating Yak Guru

283 Posts

Posted - 2008-06-03 : 02:02:19
when i run the procedure i get like:
id name g1 g2
1 aa 2 1
2 bb 1 3

i want total of gifts like
id name g1 g2
1 aa 2 1
2 bb 1 3
-------
3 4

Thanks alot..


quote:
Originally posted by visakh16

quote:
Originally posted by kwikwisi

this is storedprocedure for cross tab,i also want to add CustomerID and total of gifts base on gift type. how can i do?
Thanks..

PROCEDURE dbo.trymatrix AS
DECLARE @SQLCommand varchar(4096), @column sysname

SET @SQLCommand = 'select Name,CustomerID ' + char(13)

DECLARE curGift CURSOR STATIC LOCAL FOR

select distinct GiftTitle from Redemption1

OPEN curGift

WHILE 1 = 1

BEGIN

FETCH curGift INTO @column

IF @@FETCH_STATUS <> 0

BREAK

SET @SQLCommand = @SQLCommand +

'sum(case GiftTitle when ''' +@column +''' then 1 else 0 end) as '+ @column + ','+char(13)

END

DEALLOCATE curGift

SET @SQLCommand = left (@SQLcommand, len(@SQLcommand)-2) + char(13)

SET @SQLCommand = @SQLCommand +

'from Customer c

inner join Redemption1 d

on c.CustomerID=d.CustomerID

group by Name,CustomerID'

PRINT @SQLCommand

EXEC (@SQLCommand)


The above will give you CustomerID too
Is Gift type another field in your table?

Go to Top of Page
   

- Advertisement -