我有一個SQL表是這樣的: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]
)
但它無法正常工作。我不確定我是否瞭解數據透視表是如何工作的,以及它是否可以用於我的案例。還是有另一種方法來做到這一點? (我試圖避免環路,因爲我有1分萬人次的紀錄)
感謝
你期待返回全部100萬條記錄的結果集? (你會有1000列) – dana