Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

transpose data using sql

Status
Not open for further replies.

bombler

Technical User
Nov 13, 2003
1
AU
I would like to turn a normalised table in to an unnormalised table in the simplest possible mannor...

I have:

col1 col2
----------
A 1
A 2
A 3

and would like to output in one line:

A 1 2 3

Would like this in MSAccess97 but am open to other sql tools.

Regards,
Brent
 
There is no easy way to change rows to columns, you need a sequence number for the max number of rows the value in col1 can have. The sequence should restart each time col1 value changes.

Select Col1,
MAX(Case When SeqCol = seq1 then col2 else 0 end),
MAX(Case When SeqCol = seq2 then col2 else 0 end),
MAX(Case When SeqCol = seq3 then col2 else 0 end),
...
MAX(Case When SeqCol2 = seq then col2 else 0 end)
From Tab1
Group By col1;

If you have a small number of values for A, it isn't too bad, but the case statments must cover the highest number. The MAX on each case statment will put all A values on one row.

MS Excel handles Xtabs very well, then the data could be reimported back into MSAccess.
 
Access97 has a crosstab query type which does this for you. They are easy to set up.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top