4

Possible Duplicate:
how to select columns as rows?

I have a table with ID's, like

Table
-------
  1
  2
  3
  4
  5
  and so on

I have this query,

SELECT A,B,Here I need To SELECT the Id's as Column, FROM MyTable

So the Result of this will be,

  A  B  1  2  3  4  5  6  7 and so on
  -----------------------------------
Community
  • 1
  • 1
Imran Qadir Baksh - Baloch
  • 32,612
  • 68
  • 179
  • 322

2 Answers2

4

You've the answer already in SO :)

See here or here. When you fill the question pay attention, usually it gives you similar questions :)

Community
  • 1
  • 1
aF.
  • 64,980
  • 43
  • 135
  • 198
  • 1
    Actually, I don't think those answers cover situations where you don't know what your columns will be which is is a problem I think he has. – Brandon Moore Nov 15 '11 at 09:53
4

You want a cross tab query. Check this out: http://www.simple-talk.com/sql/t-sql-programming/creating-cross-tab-queries-and-pivot-tables-in-sql/

It explains how to do a simple cross tab query which may be all you need. However, it looks like you may also benefit from the stored procedure the writer created to overcome some shortcomings of the cross tab query.

Brandon Moore
  • 8,590
  • 15
  • 65
  • 120