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.
| Author |
Topic |
|
gmcgahey
Starting Member
13 Posts |
Posted - 2006-05-24 : 11:42:22
|
| HelloI need some help transposing data for reporting purposes. Scenario is as follows:I have an existing database table with the following format:AccountID Budget1 Budget2 Budget3GL980 6,090 7,000 4,700 I need to transpose this data into the following format:AccountID Period BudgetGL980 1 6,090GL980 2 7,000GL980 3 4,700where I 'Period' is a parameter, i.e. if Period 5 is selected, the output should show all periods from 1-5There are 13 budgetary periods in the existing table structure. Any help would be greatly appreciated! |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-05-24 : 11:49:01
|
The best solution: Normalise your table existing database table structure.http://www.datamodel.org/NormalizationRules.htmlThe temporary solution (something based on):--datadeclare @t table (AccountID varchar(10), Budget1 int, Budget2 int, Budget3 int)insert @t select 'GL980', 6090, 7000, 4700--calculationselect * from ( select AccountID, 1 as Period, Budget1 from @t union all select AccountID, 2, Budget2 from @t union all select AccountID, 3, Budget3 from @t ) awhere Period <=2 Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
gmcgahey
Starting Member
13 Posts |
Posted - 2006-05-24 : 11:57:39
|
| Thanks RyanYes I wish the database was normalised, but its not within my control. The Union select will do just finethanks |
 |
|
|
|
|
|
|
|