在 SELECT 中选择行作为列 [英] Select Rows As Column in SELECT

查看:19
本文介绍了在 SELECT 中选择行作为列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何选择列作为行?

我有一张带有 ID 的表格,例如

I have a table with ID's, like

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

我有这个问题,

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

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

所以结果是,

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

推荐答案

你想要一个交叉表查询.看看这个:http://www.simple-talk.com/sql/t-sql-programming/creating-cross-tab-queries-and-pivot-tables-in-sql/

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.

这篇关于在 SELECT 中选择行作为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆