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.
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 ASDECLARE @SQLCommand varchar(4096), @column sysnameSET @SQLCommand = 'select Name, ' + char(13)DECLARE curGift CURSOR STATIC LOCAL FORselect distinct GiftTitle from Redemption1OPEN curGiftWHILE 1 = 1BEGINFETCH curGift INTO @columnIF @@FETCH_STATUS <> 0BREAKSET @SQLCommand = @SQLCommand +'sum(case GiftTitle when ''' +@column +''' then 1 else 0 end) as '+ @column + ','+char(13)ENDDEALLOCATE curGiftSET @SQLCommand = left (@SQLcommand, len(@SQLcommand)-2) + char(13)SET @SQLCommand = @SQLCommand + 'from Customer c inner join Redemption1 don c.CustomerID=d.CustomerIDgroup by Name'PRINT @SQLCommandEXEC (@SQLCommand) |
|
ranganath
Posting Yak Master
209 Posts |
Posted - 2008-06-03 : 00:56:35
|
Hi,please post your sample data |
 |
|
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 ASDECLARE @SQLCommand varchar(4096), @column sysnameSET @SQLCommand = 'select Name,CustomerID ' + char(13)DECLARE curGift CURSOR STATIC LOCAL FORselect distinct GiftTitle from Redemption1OPEN curGiftWHILE 1 = 1BEGINFETCH curGift INTO @columnIF @@FETCH_STATUS <> 0BREAKSET @SQLCommand = @SQLCommand +'sum(case GiftTitle when ''' +@column +''' then 1 else 0 end) as '+ @column + ','+char(13)ENDDEALLOCATE curGiftSET @SQLCommand = left (@SQLcommand, len(@SQLcommand)-2) + char(13)SET @SQLCommand = @SQLCommand + 'from Customer c inner join Redemption1 don c.CustomerID=d.CustomerIDgroup by Name,CustomerID'PRINT @SQLCommandEXEC (@SQLCommand)
The above will give you CustomerID tooIs Gift type another field in your table? |
 |
|
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 g21 aa 2 12 bb 1 3i want total of gifts likeid name g1 g21 aa 2 12 bb 1 3 ------- 3 4Thanks 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 ASDECLARE @SQLCommand varchar(4096), @column sysnameSET @SQLCommand = 'select Name,CustomerID ' + char(13)DECLARE curGift CURSOR STATIC LOCAL FORselect distinct GiftTitle from Redemption1OPEN curGiftWHILE 1 = 1BEGINFETCH curGift INTO @columnIF @@FETCH_STATUS <> 0BREAKSET @SQLCommand = @SQLCommand +'sum(case GiftTitle when ''' +@column +''' then 1 else 0 end) as '+ @column + ','+char(13)ENDDEALLOCATE curGiftSET @SQLCommand = left (@SQLcommand, len(@SQLcommand)-2) + char(13)SET @SQLCommand = @SQLCommand + 'from Customer c inner join Redemption1 don c.CustomerID=d.CustomerIDgroup by Name,CustomerID'PRINT @SQLCommandEXEC (@SQLCommand)
The above will give you CustomerID tooIs Gift type another field in your table?
|
 |
|
|
|
|
|
|