如何在 sql 服务器 2008r2 中将行名称更改为列名称

How to change Row name into Column name in sql server 2008r2

我无法在 asp.net 中使用存储过程作为绑定来制作 XtraCharts 线图(XY 图)。

我想通过 R250R500R1000R2000 行生成 X 值 和 Y 值是已经存储在上面每一行中的数据。

我的起源table是这样的:

========================================================
No | Sequence No | ItemId | R250 | R500 | R1000 | R2000
========================================================
1    001           118       23     13     14      24

我要显示成

========================================================
No | Sequence No | ItemID | Value | NameX
========================================================
1    001            118      23     R250
1    001            118      13     R500
1    001            118      14     R1000
1    001            118      24     R2000

是否有任何可能的查询可以实现??非常感谢..

您可以使用 apply isspecially for this :

select t.No, t.[Sequence No], t.ItemID, tt.*
from table t cross apply
     ( values ([R250], 'R250'),
              ([R500], 'R500'),
              ([R1000],'R1000'),
              ([R2000],'R2000')
     ) tt (Value, NameX);

您只需要简单的 UNPIVOT (helpful article).

试试下面的代码:

declare @tbl table (No int, SequenceNo varchar(3), ItemId int, R250 int, R500 int, R1000 int, R2000 int);
insert into @tbl values (1,'001', 118, 23, 13, 14, 24);

select * from (
    select * from @tbl
) p unpivot (
    [Value] for NameX in (R250, R500, R1000, R2000)
) as up;