我有一个这样的 SQL 表:
Animal1 Animal2 Corelation
---------+---------------+--------------
Cat Cat 1
Cat Dog 0.6
Cat Mouse 0.8
Dog Cat 0.6
Dog Dog 1
Dog Mouse 0.4
Mouse Cat 0.8
Mouse Dog 0.4
Mouse Mouse 1
我正在寻找一个 SQL 查询来返回以下结果:
Animal 1 Cat Dog Mouse
---------+---------------+------------------+---------------+
Cat 1 0.6 0.8
Dog 6 1 0.4
Mouse 0.8 0.4 1
基本上我想要更易读的表格版本。
我尝试像这样使用枢轴:
use SymbolsDB
select * from [AnimalsTable]
pivot (
[Corelation]
for [Animal2] in (select * from [Animal2]
)
但它不起作用。我不确定我是否了解枢轴的工作原理以及是否可以在我的情况下使用它。还是有其他方法可以做到这一点?(我试图避免循环,因为我有 100 万条记录)
谢谢