sql server - How do I use Dynamic SQL to Generate Groups of Columns? -
people different types , amounts of fruit , want them. have table looks this:
name fruit count temp ----------------------------- jim apple 3 hot jim banana 7 cold jim orange 12 cold sam plum 5 hot sam peach 1 hot bob cherry 4 cold bob banana 11 hot bob orange 9 cold bob kiwi 6 hot
each person have 1 or 1000 rows , not know how many ahead of time think requires dynamic column lists. need this:
name fruit_1 count_1 temp_1 fruit_2 count_2 temp_2 fruit_3 count_3 temp_3 fruit_4 count_4 temp_4 ------------------------------------------------------------------------------------------------------- jim apple 3 hot banana 7 cold orange 12 cold null null null sam plum 5 hot peach 1 hot null null null null null null bob cherry 4 cold banana 11 hot orange 9 cold kiwi 6 hot
the code pivot in sql 2005 works parsing out 1 column when don't know how many rows there be, can't work multiple columns. tried creating @select_list
variables, , can use create columns, can't data want in them , don't know how interweave order want (eg f1,c1,t1,f2,c2,t2 vs f1,f2,c1,c2,t1,t2 etc)
here code have been unsuccessfully trying tweak:
create table #stackoverflowtest( [name] [varchar](3) not null, [fruit] [varchar](12) not null, [number] [int] not null, [temp] [varchar](4) not null) insert #stackoverflowtest values ('jim','apple',3,'hot'), ('jim','banana',7,'cold'), ('jim','orange',12,'cold'), ('sam','plum',5,'hot'), ('sam','peach',1,'hot'), ('bob','cherry',4,'cold'), ('bob','banana',11,'hot'), ('bob','orange',9,'cold'), ('bob','kiwi',6,'hot') declare @sql varchar(max) declare @pivot_list varchar(max) declare @select_list varchar(max) select @pivot_list = coalesce(@pivot_list + ', ', '') + '[' + convert(varchar, pivot_code) + ']' ,@select_list = coalesce(@select_list + ', ', '') + '[' + convert(varchar, pivot_code) + '] [fruit_' + convert(varchar, pivot_code) + ']' ( select distinct pivot_code ( select name, fruit, number, temp, row_number() on (partition name order name,number) pivot_code #stackoverflowtest ) rows ) pivot_codes set @sql = ' ;with p ( select name, fruit, number, temp, row_number() on (partition name order name,number) pivot_code #stackoverflowtest ) select name, ' + @select_list + ' p pivot ( min(fruit) pivot_code in ( ' + @pivot_list + ' ) ) pvt ' exec (@sql)
i wouldn't try using pivot that, gets headmess situation pretty quickly.
this simpler:
declare @sql varchar(max) declare @select_list varchar(max) select @select_list = isnull(@select_list + ', ', '') + 'sum(case when pivot_code = ' + [pivot_code] + ' number else null end) number_' + [pivot_code] + ', min(case when pivot_code = ' + [pivot_code] + ' temp else null end) temp_' + [pivot_code] + ', min(case when pivot_code = ' + [pivot_code] + ' fruit else null end) fruit_' + [pivot_code] ( select distinct pivot_code ( select name, fruit, number, temp, convert(varchar(20), row_number() on (partition name order name,number)) pivot_code #stackoverflowtest ) rows ) pivot_codes set @sql = ' ;with p ( select name, fruit, number, temp, row_number() on (partition name order name,number) pivot_code #stackoverflowtest ) select name, ' + @select_list + ' p group name' exec (@sql)
Comments
Post a Comment