7
我寫了這個查詢的不同值的不確定量:樞軸與
SELECT s, [1] AS a1, [2] AS a2, [3] AS a3, [4] AS a4
FROM (SELECT grade, aid, s FROM m) p
PIVOT
(
SUM(grade)
FOR aid IN ([1], [2], [3], [4])
) AS pvt ORDER BY pvt.s;
返回結果:
s a1 a2 a3 a4
1 25 69 95 56
2 27 99 16 87
. . . .
99 98 12 34 76
這正是我想要的結果。我的問題是,'援助'中不會總是有四個不同的值。是否有可能重寫此查詢(或使用存儲過程),以便'a *'列的數量取決於'aid'中有多少個不同的值?
哇謝謝,這正是我想要的! –
+1 - 雖然很挑剔,但您錯過了列的別名 – Lamak