SQL 在SELECT中选择行作为列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8134063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 13:15:30  来源:igfitidea点击:

Select Rows As Column in SELECT

sqlsql-servertsqlpivot

提问by user960567

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

SELECT A,B,这里我需要从 MyTable 中选择 Id 作为列

So the Result of this will be,

所以结果将是,

  A  B  1  2  3  4  5  6  7 and so on
  -----------------------------------

采纳答案by Brandon Moore

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/

您需要交叉表查询。看看这个: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.

它解释了如何进行简单的交叉表查询,这可能就是您所需要的。但是,看起来您也可以从作者创建的存储过程中受益,该存储过程克服了交叉表查询的一些缺点。

回答by aF.

You've the answer already in SO :)

您已经在 SO 中找到了答案:)

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

请参阅此处此处。当你填写问题时注意,通常它会给你类似的问题:)