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 |
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2006-07-21 : 09:49:11
|
| hiI have a table tblDemo which i want to change from:TCode TransDa Amount----- ------ ------AB001 2006001 1000AB001 2006001 1100AB002 2006001 1020AB002 2006001 1150AB001 2006002 1200AB001 2006002 1210AB002 2006002 1110AB002 2006002 2000to:TCode TransDa Amount AB001-2006001 AB002-2006001 AB001-2006002 AB002-2006002----- ------- ------ ------------- ------------ ------------- -------------AB001 2006001 1000 1000 0 0 0AB001 2006001 1100 1100 0 0 0AB002 2006001 1020 0 1020 0 0AB002 2006001 1150 0 1150 0 0AB001 2006002 1200 0 0 1200 0AB001 2006002 1210 0 0 1210 0AB002 2006002 1110 0 0 0 1110AB002 2006002 2000 0 0 0 2000It looks more like permutation between TCode and TransDa. Any help many thanks in advance. Thanks |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-07-21 : 10:42:04
|
Same pivot / cross-tab technique as beforedeclare @table table( TCode varchar(10), TransDa int, Amount int)insert into @tableselect 'AB001', 2006001, 1000 union allselect 'AB001', 2006001, 1100 union allselect 'AB002', 2006001, 1020 union allselect 'AB002', 2006001, 1150 union allselect 'AB001', 2006002, 1200 union allselect 'AB001', 2006002, 1210 union allselect 'AB002', 2006002, 1110 union allselect 'AB002', 2006002, 2000select TCode, TransDa, case when TCode = 'AB001' and TransDa = 2006001 then Amount else 0 end as [AB001-2006001], case when TCode = 'AB002' and TransDa = 2006001 then Amount else 0 end as [AB002-2006001], case when TCode = 'AB001' and TransDa = 2006002 then Amount else 0 end as [AB001-2006002], case when TCode = 'AB002' and TransDa = 2006002 then Amount else 0 end as [AB002-2006002]from @table/* RESULTTCode TransDa AB001-2006001 AB002-2006001 AB001-2006002 AB002-2006002 ---------- ----------- ------------- ------------- ------------- ------------- AB001 2006001 1000 0 0 0AB001 2006001 1100 0 0 0AB002 2006001 0 1020 0 0AB002 2006001 0 1150 0 0AB001 2006002 0 0 1200 0AB001 2006002 0 0 1210 0AB002 2006002 0 0 0 1110AB002 2006002 0 0 0 2000*/ KH |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2006-07-21 : 11:13:21
|
| Yes, thank and which i had tried before i did a post and it work and also sorry for not posting clearly. Actually i was trying to make this while statement work.I tried this: but it didn't work. thanks-- Declare Variables for the use of calculations.. Declare @sQry Varchar(8000)-- Declare Variables for the use of calculations.. Declare @sQry Varchar(8000)Declare @date int Declare @TCode as varchar(15)Select @Date = 0,@sQry = 'Select * , ', @TCode = '0'-- Loop for creating the Dynamic SQL While @Date < (Select Distinct Max(TransDate) From TblDemo) and @TCode < (Select Distinct Max(TCode) From TblDemo) Begin Select @Date = Min(TransDa) From Tmp Where TransDate > @DateSelect @TCode = Min(TCode) From Tmp Where TCode > @TCode Select @sQry = @sQry + ' Case When TransDate = ' + Convert(varchar,@Date) + ' and TCode= ''@TCode''' + ' Then Amount Else 0 End As [' + @TCode + - + Convert(varchar,@Date) + '],'End |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-07-21 : 11:40:17
|
If you were trying to create a dynamic pivot table, you can do so like this...--datacreate table #table (TCode varchar(10), TransDa int, Amount int)insert #tableselect 'AB001', 2006001, 1000 union allselect 'AB001', 2006001, 1100 union allselect 'AB002', 2006001, 1020 union allselect 'AB002', 2006001, 1150 union allselect 'AB001', 2006002, 1200 union allselect 'AB001', 2006002, 1210 union allselect 'AB002', 2006002, 1110 union allselect 'AB002', 2006002, 2000--calculationdeclare @s varchar(8000)select @s = isnull(@s + ',' + char(13), '') + ' case when TCode = ''' + TCode + ''' and TransDa = ' + cast(TransDa as varchar(10)) + ' then Amount else 0 end as [' + TCode + '-' + cast(TransDa as varchar(10)) + ']'from #table group by TCode, TransDaexec('select TCode, TransDa,' + @s + 'from #table order by TCode, TransDa')/*resultsTCode TransDa AB001-2006001 AB001-2006002 AB002-2006001 AB002-2006002 ---------- ----------- ------------- ------------- ------------- ------------- AB001 2006001 1000 0 0 0AB001 2006001 1100 0 0 0AB001 2006002 0 1200 0 0AB001 2006002 0 1210 0 0AB002 2006001 0 0 1020 0AB002 2006001 0 0 1150 0AB002 2006002 0 0 0 1110AB002 2006002 0 0 0 2000*/--tidy updrop table #tableRyan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2006-07-21 : 11:45:26
|
| Thank a million thanks. |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2006-07-22 : 00:00:19
|
| I tried doing a select into statement with this:declare @s varchar(8000)select @s = isnull(@s + ',' + char(13), '') + ' case when TCode = ''' + TCode + ''' and TransDa = ' + cast(TransDa as varchar(10)) + 'and LedType = ''Actual''' + ' then Amount else 0 end as [' + TCode + '-' + cast(TransDa as varchar(10)) + ']'from #table group by TCode, TransDaSelect @s = @s + 'into tblDemo' -- from #tableexec('select TCode,Amount, LedType, TransDa,' + @s + 'from #table order by TCode, TransDa')But don't seem to work. I don't where gone wrong. Thanks |
 |
|
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2006-07-22 : 00:03:21
|
| Ok I have done it with this code:declare @s varchar(8000)select @s = isnull(@s + ',' + char(13), '') + ' case when TCode = ''' + TCode + ''' and TransDa = ' + cast(TransDa as varchar(10)) + 'and LedType = ''Actual''' + ' then Amount else 0 end as [' + TCode + '-' + cast(TransDa as varchar(10)) + ']'from #table group by TCode, TransDaexec('select * ,' + @s + 'into tblDemo from #table order by TCode, TransDa')Thanks a million thanks anyway. |
 |
|
|
|
|
|
|
|