2

我对使用有点陌生pivot。我怎样才能从这个中枢轴这个记录:

P1AVERAGE    P2AVERAGE    P3AVERAGE   -> column name
1.25         1.50         1.75

对此:

AVERAGENAME    AVERAGESCORE
p1Average      1.25
p2Average      1.50
p3Average      1.75

我同时使用 SQL Server 和 MS Access 2007。

4

2 回答 2

6

你想要做的UNPIVOT不是PIVOT,它使列重新变成行,它与枢轴相反。

对于 SQL Server,使用UNPIVOT表运算符:

SELECT *
FROM tablename AS t
UNPIVOT
(
  AVERAGESCORE
  FOR plaveragename IN([P1AVERAGE], [P2AVERAGE], [P3AVERAGE])
) AS u;

在这里查看它的实际效果:

于 2013-05-17T15:36:09.757 回答
2

您应该能够在 SQL Server 和 MS Access 中使用以下查询。UNION ALL 获取列并将值转换为多行:

select 'P1AVERAGE' as AverageName, P1AVERAGE as AverageScore
from yourtable
union all
select 'P2AVERAGE' as AverageName, P2AVERAGE as AverageScore
from yourtable
union all
select 'P3AVERAGE' as AverageName, P3AVERAGE as AverageScore
from yourtable
于 2013-05-17T15:37:42.903 回答