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
 Is this a transpose?

Author  Topic 

wooldawg
Starting Member

12 Posts

Posted - 2008-05-02 : 17:14:47
Hello,

I have a table that looks like this:

Customer Treas
------- -----
1010 Merch
1010 Price
1020 Merch
1030 Promo

And I need to add a period column so that the table looks like this.

Customer Treas Period
------- ----- ------
1010 Merch 1
1010 Merch 2
1010 Merch 3
1010 Price 1
1010 Price 2
1010 Price 3
1020 Merch 1
1020 Merch 2
1020 Merch 3
1030 Promo 1
1030 Promo 2
1030 Promo 3

etc. Of course there are 12 periods.

I'm thinking this is related to cross-tab or transpose function somehow? Once again I am clueless to the point I find it difficult to even research the problem on my own.

Hopefully this makes sense.

Thanks!


TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-05-02 : 17:28:11
Not sure what exactly your question is but maybe this will help:

--starting point
create table #t (Customer int, Treas varchar(10))
insert #t
select 1010, 'Merch' union all
select 1010, 'Price' union all
select 1020, 'Merch' union all
select 1030, 'Promo'
go

--add the column
alter table #t add period int null
go
--update the data for period=1
update #t set period = 1

--generate the remaining periods (using a table that has a bunch of numbers in it)
insert #t (customer, Treas, Period)
select customer
,Treas
,number
from #t t
cross join master..spt_values v
where v.type = 'P'
and v.number between 2 and 12

select * from #t

drop table #t


Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -