過去已經很好地回答了這個問題(here和here)。看來,我仍然無法讓我知道這個關鍵的東西(我也沒有使用它很長一段時間)。如何在SQL Server 2008中透視數據以獲得期望的結果?
所以希望有人(再次)能告訴我如何將數據放到所需的格式:
鑑於數據:
ID | Label | Occurences | RangeBegin | RangeEnd | Unit
---+-------+------------+------------+----------+--------
1 | One | 0 | -1000 | 0 | m
1 | One | 5 | 0 | 10 | m
1 | One | 8 | 10 | 20 | m
1 | One | 6 | 20 | 30 | m
1 | One | 15 | 30 | 40 | m
1 | One | 0 | 40 | 1000 | m
2 | One | 0 | -1000 | 0 | m
2 | One | 2 | 0 | 10 | m
2 | One | 13 | 10 | 20 | m
2 | One | 27 | 20 | 30 | m
2 | One | 5 | 30 | 40 | m
2 | One | 0 | 40 | 1000 | m
1 | Two | 0 | -1000 | 0 | kg
1 | Two | 4 | 0 | 2 | kg
1 | Two | 6 | 2 | 4 | kg
1 | Two | 1 | 4 | 6 | kg
1 | Two | 0 | 6 | 1000 | kg
2 | Two | 0 | -1000 | 0 | kg
2 | Two | 8 | 0 | 2 | kg
2 | Two | 1 | 2 | 4 | kg
2 | Two | 3 | 4 | 6 | kg
2 | Two | 0 | 6 | 1000 | kg
期望的結果:
ID | One | OneRangeBegin | OneRangeEnd | OneUnit | Two | TwoRangeBegin | TwoRangeEnd | TwoUnit
---+-----+---------------+-------------+---------+------+---------------+-------------+----------
1 | 0 | -1000 | 0 | m | 0 | -1000 | 0 | kg
1 | 5 | 0 | 10 | m | 4 | 0 | 2 | kg
1 | 8 | 10 | 20 | m | 6 | 2 | 4 | kg
1 | 6 | 20 | 30 | m | 1 | 4 | 6 | kg
1 | 15 | 30 | 40 | m | 0 | 6 | 1000 | kg
1 | 0 | 40 | 1000 | m | null | null | null | null
2 | 0 | -1000 | 0 | m | 0 | -1000 | 0 | kg
2 | 2 | 0 | 10 | m | 8 | 0 | 2 | kg
2 | 13 | 10 | 20 | m | 1 | 2 | 4 | kg
2 | 27 | 20 | 30 | m | 3 | 4 | 6 | kg
2 | 5 | 30 | 40 | m | 0 | 6 | 1000 | kg
2 | 0 | 40 | 1000 | m | null | null | null | null
而且爲了使它的工作更容易一點,我把上面的數據放在SqlFiddle。
兩個表你需要看看逆轉置和支點。你有沒有嘗試過任何代碼來獲得解決方案? – Taryn
發佈您的查詢嘗試,並發現它出了什麼問題。錯誤或意外結果? –