I doubt this is what you want but assuming you only want 3 industries for any acctid (it would be tough to allow for a varied number of columns)--this represents your tabledeclare @t table (acctid int, industry varchar(10))insert @t(ACCTID , Industry)select 123, 'Auto' union allselect 123, 'Bike' union allselect 333, 'Train' union allselect 444, 'Bike' union allselect 555, 'Auto' union allselect 555, 'Bike' union allselect 555, 'Train'--create a helper #tempp tablecreate table #just3 (acctid int, industry varchar(10), rowid int, primary key clustered (acctid, industry))--populate the helper table (with just 3 industries for each acctid)insert #just3 (acctid, industry)select acctid, industry from @t t where industry in (select top 3 industry from @t where acctid = t.acctid order by industry)order by acctid, industry--populate our rowid column declare @r int, @a varchar(10)select @a = '', @r = 1update #just3 set @r = rowid = case when acctid = @a then @r + 1 else 1 end ,@a = acctid--final select statementselect acctid ,industry1 = max(case when rowid = 1 then industry end) ,industry2 = max(case when rowid = 2 then industry end) ,industry3 = max(case when rowid = 3 then industry end)from #just3group by acctiddrop table #just3output:acctid industry1 industry2 industry3 ----------- ---------- ---------- ---------- 123 Auto Bike NULL333 Train NULL NULL444 Bike NULL NULL555 Auto Bike Train
Be One with the OptimizerTG