Solved

SQL Table Transpose

Posted on 2007-04-09
7
443 Views
Last Modified: 2012-06-22
Is there a command in sql to transpose a table?
0
Comment
Question by:kayhustle
  • 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

757 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

20 Experts available now in Live!

Get 1:1 Help Now