Solved

SQL Table Transpose

Posted on 2007-04-09
7
448 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

717 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