數據表:
ID NAME 1 蘋果汁 2 牛奶 3 蕃茄醬 4 鹽 5 麻油 6 醬油 7 海鮮粉 8 胡椒粉 9 雞 10 蟹 11 民眾奶酪 12 德國奶酪 13 龍蝦 14 沙茶 15 味精 16 餅干 17 豬肉 18 墨魚 19 糖果 20 桂花糕 21 花生 22 糯米
欲得到的結果:
ID1 NAME1 ID2 NAME2 ID3 NAME3 1 蘋果汁 2 牛奶 3 蕃茄醬 4 鹽 5 麻油 6 醬油 7 海鮮粉 8 胡椒粉 9 雞 10 蟹 11 民眾奶酪 12 德國奶酪 13 龍蝦 14 沙茶 15 味精 16 餅干 17 豬肉 18 墨魚 19 糖果 20 桂花糕 21 花生 22 糯米
使用SQL語句:
WITH TEST AS(
SELECT 1 AS Id, ''蘋果汁'' AS NAME FROM Dual UNION ALL
SELECT 2 AS Id, ''牛奶'' AS NAME FROM Dual UNION ALL
SELECT 3 AS Id, ''蕃茄醬'' AS NAME FROM Dual UNION ALL
SELECT 4 AS Id, ''鹽'' AS NAME FROM Dual UNION ALL
SELECT 5 AS Id, ''麻油'' AS NAME FROM Dual UNION ALL
SELECT 6 AS Id, ''醬油'' AS NAME FROM Dual UNION ALL
SELECT 7 AS Id, ''海鮮粉'' AS NAME FROM Dual UNION ALL
SELECT 8 AS Id, ''胡椒粉'' AS NAME FROM Dual UNION ALL
SELECT 9 AS Id, ''雞'' AS NAME FROM Dual UNION ALL
SELECT 10 AS Id, ''蟹'' AS NAME FROM Dual UNION ALL
SELECT 11 AS Id, ''民眾奶酪'' AS NAME FROM Dual UNION ALL
SELECT 12 AS Id, ''德國奶酪'' AS NAME FROM Dual UNION ALL
SELECT 13 AS Id, ''龍蝦'' AS NAME FROM Dual UNION ALL
SELECT 14 ASId, ''沙茶'' AS NAME FROM Dual UNION ALL
SELECT 15 AS Id, ''味精'' AS NAME FROM Dual UNION ALL
SELECT 16 AS Id, ''餅干'' AS NAME FROM Dual UNION ALL
SELECT 17 AS Id, ''豬肉'' AS NAME FROM Dual UNION ALL
SELECT 18 AS Id, ''墨魚'' AS NAME FROM Dual UNION ALL
SELECT 19 AS Id, ''糖果'' AS NAME FROM Dual UNION ALL
SELECT 20 AS Id, ''桂花糕'' AS NAME FROM Dual UNION ALL
SELECT 21 AS Id, ''花生'' AS NAME FROM Dual UNION ALL
SELECT 22 AS Id, ''糯米'' AS NAME FROM Dual
)
SELECT MAX(Decode(MOD(Id, 3), 1, Id, '''')) Id1,
MAX(Decode(MOD(Id, 3), 1, NAME, '''')) Name1,
MAX(Decode(MOD(Id, 3), 2, Id, '''')) Id2,
MAX(Decode(MOD(Id, 3), 2, NAME, '''')) Name2,
MAX(Decode(MOD(Id, 3), 0, Id, '''')) Id3,
MAX(Decode(MOD(Id, 3), 0, NAME, '''')) Name3
FROM Test
GROUP BY Trunc(Rownum / 3 - 0.1, 0)
ORDER BY Id1