由於查詢中缺少額外的列使聚合變得困難,我無法看到如何使用主元。如果你確實有其他列,那麼樞軸將會減少代碼消耗;但我不是樞軸專家。你可以這樣做很輕鬆地與幾個加入......用我的理貨表來生成一個整數列表
SELECT
aa.StaticInteger as A,
bb.StaticInteger as B,
cc.StaticInteger as C,
dd.StaticInteger as D
FROM
tblTally aa
LEFT OUTER JOIN
(
SELECT
StaticInteger
FROM
tblTally
WHERE
StaticInteger BETWEEN 9 AND 16
) bb
ON
aa.StaticInteger = bb.StaticInteger - 8
LEFT OUTER JOIN
(
SELECT
StaticInteger
FROM
tblTally
WHERE
StaticInteger BETWEEN 17 AND 24
) cc
ON
bb.StaticInteger = cc.StaticInteger - 8
LEFT OUTER JOIN
(
SELECT
StaticInteger
FROM
tblTally
WHERE
StaticInteger BETWEEN 25 AND 32
) dd
ON
cc.StaticInteger = dd.StaticInteger - 8
WHERE
aa.StaticInteger BETWEEN 1 AND 8
返回
A B C D
1 9 17 25
2 10 18 26
3 11 19 27
4 12 20 28
5 13 21 29
6 14 22 30
7 15 23 31
8 16 24 32
你見過'PIVOT'條款嗎? –
沒有。我不支持 – Anjali