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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting rows to columns

Status
Not open for further replies.

AOLBoy

IS-IT--Management
May 7, 2002
68
GB
My SQL exposure is relatively basic. I was wondering if it is possible to build a command that can retrieve data from a table into new columns.

I have a table with the following columns:

AccountNo
Ccy
Amount

where the Ccy is always either GBP or USD.

I want to create an SQL statement that can return me a new table as follows:

AccountNo
AmountGBP
AmountUSD

Can this be done with one command?







 
as mysql does not support SELECT ... INTO statement with creation of new table you have to use the CREATE ... SELECT statement

e.g.
Code:
CREATE TABLE NewTable (AccountNo int, AmountGBP decimal, AmountUSD decimal) SELECT AccountNo, CASE WHEN Ccy = 'GBP' THEN Amount ELSE NULL END, CASE WHEN Ccy = 'USD' THEN Amount ELSE NULL END FROM OldTable
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top