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
 General SQL Server Forums
 New to SQL Server Programming
 Dynamic columns

Author  Topic 

Ranjit.ileni
Posting Yak Master

183 Posts

Posted - 2011-08-26 : 05:32:20
[code]
create table #BaseTable
(
Id int identity
,Metadata Varchar(100)
)
insert into #BaseTable
select 'tiffin' union all
select 'lunch' union all
select 'snacks' union all
select 'dinner' union all
select 'Tea'


create table #temp
(
Id int identity
,Rid int
,Metadata Varchar(100)
,PaxCount int
,comment varchar(100)
)
insert into #temp
select 10,'tiffin',1,'needed' union all
select 10,'lunch',2,'not needed' union all
select 10,'snacks',35,'not interested' union all
select 10,'dinner',5,'ok' union all
select 11,'dinner',4,'ok'


select * from #BaseTable

select * from #temp

Here #BaseTable have metadata values ,we can enter multiple rows for metadata
and #temp Table uses the metadata as a foreignkey ref.

expected output:
columns names should be dynamic:
Rid tiffin comment lunch comment snacks comment dinner comment Tea comment
[/code]
Rid tiffin comment lunch comment snacks comment dinner comment Tea comment
10 1 needed 2 not needed 35 not interested 5

--Ranjit

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-08-26 : 06:36:25
see
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -