我在SQL中有一个这样的表(顺序不正确)

A B         VAL                  VAL2
---------------------------------------------
1 1 -2.1846000000000e+02  0.0000000000000e+00
2 2 -2.1846000000000e+02  0.0000000000000e+00
3 2  6.4000000000000e+01  0.0000000000000e+00
1 2  6.4000000000000e+01  0.0000000000000e+00
2 3  6.4000000000000e+01  0.0000000000000e+00
3 3 -2.1846000000000e+02  0.0000000000000e+00
3 1  6.4000000000000e+01  0.0000000000000e+00
2 1  6.4000000000000e+01  0.0000000000000e+00
1 3  6.4000000000000e+01  0.0000000000000e+00


所以每个元组A,B代表矩阵的一个点
我如何将其转换为实际矩阵

A1                      A2                      A3
--------------------------------------------------------------------
-2.1846000000000e+02    6.4000000000000e+01     6.4000000000000e+01
6.4000000000000e+01     -2.1846000000000e+02    6.4000000000000e+01
6.4000000000000e+01     6.4000000000000e+01    -2.1846000000000e+02

最佳答案

给定表M(x,y,v)

SELECT x, [1], [2], [3], [4], ...
FROM
(SELECT x, y, v FROM M) AS s
PIVOT
(
min(v)
FOR y IN ([1], [2], [3], [4], ...)
) AS pvt
order by x


, ...表示根据需要继续该系列,但不要将其保留在脚本中

演示:http://www.sqlfiddle.com/#!3/7ee1e/17(感谢mellamokb的安装脚本)

关于sql-server - 将垂直转换为水平布局sql,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10321404/

10-16 03:14