SQL Table Transpose

Posted on 2007-04-09
Last Modified: 2012-06-22
Is there a command in sql to transpose a table?
Question by:kayhustle
  • 3
  • 2
  • 2
LVL 30

Expert Comment

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.

Author Comment

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

Expert Comment

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?
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud


Author Comment

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.
LVL 23

Expert Comment

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...

Author Comment

ID: 18884254
If I have
SELECT Date, Cost, Clicks, Impressions
FROM MyTable

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

Accepted Solution

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:

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need to update TableA to TableB 6 35
Need a starter for ETL protocol? 4 44
ms sql last 8 weeks as columns 5 29
MySQL Error Code 2 7
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

864 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now