Solved

SQL Table Transpose

Posted on 2007-04-09
7
447 Views
Last Modified: 2012-06-22
Is there a command in sql to transpose a table?
0
Comment
Question by:kayhustle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 30

Expert Comment

by:nmcdermaid
ID: 18880039
By transpose do you mean a crosstab?

There is a command in SQL 2005 called PIVOT

In SQL 2000 there is are many workaround solutions. The best solution depends on your actual issue and what the realted systems are.


But basically a crosstab is best done in the front end, if you have one.
0
 
LVL 1

Author Comment

by:kayhustle
ID: 18880057
I've never heard the term cross tab, but I'll look in the PIVOT command.
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 18881100
<<Is there a command in sql to transpose a table?>>
I am not familiar with that term.  Could you explain what you are expecting?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:kayhustle
ID: 18883809
Transpose is when you take a table and flip it 45 degrees so that all the columns are now rows and all rows are now columns.
0
 
LVL 23

Expert Comment

by:Racim BOUDJAKDJI
ID: 18883914
<<Transpose is when you take a table and flip it 45 degrees so that all the columns are now rows and all rows are now columns.>>
Then indeed PIVOT/UNPIVOT is what you are looking for...

Hope this helps...
0
 
LVL 1

Author Comment

by:kayhustle
ID: 18884254
If I have
SELECT Date, Cost, Clicks, Impressions
FROM MyTable
GROUP BY Date

How can I pivot that statement to return each column as an individual Date?
Thanks
0
 
LVL 30

Accepted Solution

by:
nmcdermaid earned 500 total points
ID: 18886833
OK. First thing is that wiht PIVOT you need to explicilty define the output columns. In other words you need to know which dates you want to show beforehand.

There are stored procedures around that will do this for you too.

But really you are much better off doing this in the user interface, not in the database.

Here is a link to a dynamic crosstab stord procedure:

http://weblogs.sqlteam.com/jeffs/articles/5120.aspx


Here is link backing up the fact that you should do this in the presentation layer:

http://weblogs.sqlteam.com/jeffs/archive/2005/05/12/5127.aspx
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to shrink a transaction log file down to a reasonable size.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question