Drop Table

Support Forum for database administrators and web based access to important newsgroups related to databases
Register on Database Support Forum Edit your profileCalendarFind other Database Support forum membersFrequently Asked QuestionsSearch this forum -> 
For Database admins: Free Database-related Magazines Now Free shipping to Texas


Post New Thread










Thread
Author

Transpose Rows to Columns ?
I have records

ID   Sku     Name        Date
2     41     Blair      01/04/03
3     45     John       03/04/03

that should look like...

ID       2       3
Sku     41      45
Name   Blair  John
....

and so on. Number of source rows will be fixed (12) so no of target
columns will be 12 too.

Anyone know of a quick way to do this via simple SQL ?


Report this thread to moderator Post Follow-up to this message
Old Post
bogtom@gmail.com
03-30-06 01:26 AM


Re: Transpose Rows to Columns ?
bogtom@gmail.com  wrote:
> I have records
>
> ID   Sku     Name        Date
> 2     41     Blair      01/04/03
> 3     45     John       03/04/03
>
> that should look like...
>
> ID       2       3
> Sku     41      45
> Name   Blair  John
> ....
>
> and so on. Number of source rows will be fixed (12) so no of target
> columns will be 12 too.
>
> Anyone know of a quick way to do this via simple SQL ?
>
This should work on all RDBMS:
CREATE TABLE Sales(Year    INTEGER,
Quarter INTEGER,
Results INTEGER);
SELECT Year,
MAX(CASE WHEN Quarter = 1
THEN Results END) AS Q1,
MAX(CASE WHEN Quarter = 2
THEN Results END) AS Q2,
MAX(CASE WHEN Quarter = 3
THEN Results END) AS Q3,
MAX(CASE WHEN Quarter = 4
THEN Results END) AS Q4
FROM Sales
GROUP BY Year

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Report this thread to moderator Post Follow-up to this message
Old Post
Serge Rielau
03-30-06 01:26 AM


Re: Transpose Rows to Columns ?
I understand SQL 2005 T-SQL has a Pivot command.  Would that do it?


Report this thread to moderator Post Follow-up to this message
Old Post
jim_geissman@countrywide.com
03-30-06 08:25 AM


Re: Transpose Rows to Columns ?
jim_geissman@country
wide.com  wrote:
> I understand SQL 2005 T-SQL has a Pivot command.  Would that do it?
>
OP didn't state the version....

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

Report this thread to moderator Post Follow-up to this message
Old Post
Serge Rielau
03-30-06 08:25 AM


Sponsored Links





Last Thread Next Thread
Post New Thread

Microsoft SQL Server forum archive

Show a Printable Version Email This Page to Someone! Receive updates to this thread
Microsoft SQL Server
Access database support
PostgreSQL Replication
SQL Server ODBC
FoxPro Support
PostgreSQL pgAdmin
SQL Server Clustering
MySQL ODBC
Web Applications with dBASE
SQL Server CE
MySQL++
Sybase Database Support
MS SQL Full Text Search
PostgreSQL Administration
SQL Anywhere support
DB2 UDB Database
Paradox Database Support
Filemaker Database
Berkley DB
SQL 2000/2000i database
ASE Database
Forum Jump:
All times are GMT. The time now is 08:09 PM.

 
Mobile devices forum | Database support forum archive




Copyrights DropTable.com Database Support Forum 2004 - 2006