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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Some type of pivot

Author  Topic 

chrispy
Posting Yak Master

107 Posts

Posted - 2008-04-28 : 18:49:02
Currently I have a temp table in a SP that has the following


ACCTID Industry
123 Auto
123 Bike
333 Train
444 Bike
555 Auto
555 Bike
555 Train


What I am trying to get is such

AcctID Industry_1 Industry_2 Industry_3
123 Auto Bike null
333 Train null null
444 Bike null null
555 Auto Bike Train




The Industry column could be any text and not limited to the auto,bike train as in the example.

I am at ends and have read all I can on cross tabs, pivot (this is SQL 2000) etc and cannot find a solution.

Any help would be appreciated.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-04-29 : 00:22:40
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 table
declare @t table (acctid int, industry varchar(10))
insert @t(ACCTID , Industry)
select 123, 'Auto' union all
select 123, 'Bike' union all
select 333, 'Train' union all
select 444, 'Bike' union all
select 555, 'Auto' union all
select 555, 'Bike' union all
select 555, 'Train'

--create a helper #tempp table
create 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 = 1

update #just3 set
@r = rowid = case when acctid = @a then @r + 1 else 1 end
,@a = acctid

--final select statement
select 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 #just3
group by acctid

drop table #just3

output:
acctid industry1 industry2 industry3
----------- ---------- ---------- ----------
123 Auto Bike NULL
333 Train NULL NULL
444 Bike NULL NULL
555 Auto Bike Train


Be One with the Optimizer
TG
Go to Top of Page

chrispy
Posting Yak Master

107 Posts

Posted - 2008-04-29 : 15:05:10
TG,

Thanks!! Not exactly the solution but more then enough ideas and examples that I got what I needed from it and was able to make it work.

Thanks again,
Chris
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-04-29 : 16:10:39
quote:
Originally posted by chrispy

TG,

Thanks!! Not exactly the solution but more then enough ideas and examples that I got what I needed from it and was able to make it work.

Thanks again,
Chris



That's the spirit!!

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -