首页 > 数据库 > Oracle > 正文

深入讲解Oracle数据库的多栏输出语句

2024-08-29 13:52:10
字体:
来源:转载
供稿:网友
数据表:

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 ALLSELECT 2 AS Id, '牛奶' AS NAME FROM Dual UNION ALLSELECT 3 AS Id, '蕃茄酱' AS NAME FROM Dual UNION ALLSELECT 4 AS Id, '盐' AS NAME FROM Dual UNION ALLSELECT 5 AS Id, '麻油' AS NAME FROM Dual UNION ALLSELECT 6 AS Id, '酱油' AS NAME FROM Dual UNION ALLSELECT 7 AS Id, '海鲜粉' AS NAME FROM Dual UNION ALLSELECT 8 AS Id, '胡椒粉' AS NAME FROM Dual UNION ALLSELECT 9 AS Id, '鸡' AS NAME FROM Dual UNION ALLSELECT 10 AS Id, '蟹' AS NAME FROM Dual UNION ALLSELECT 11 AS Id, '民众奶酪' AS NAME FROM Dual UNION ALLSELECT 12 AS Id, '德国奶酪' AS NAME FROM Dual UNION ALLSELECT 13 AS Id, '龙虾' AS NAME FROM Dual UNION ALLSELECT 14 AS Id, '沙茶' AS NAME FROM Dual UNION ALLSELECT 15 AS Id, '味精' AS NAME FROM Dual UNION ALLSELECT 16 AS Id, '饼干' AS NAME FROM Dual UNION ALLSELECT 17 AS Id, '猪肉' AS NAME FROM Dual UNION ALLSELECT 18 AS Id, '墨鱼' AS NAME FROM Dual UNION ALLSELECT 19 AS Id, '糖果' AS NAME FROM Dual UNION ALLSELECT 20 AS Id, '桂花糕' AS NAME FROM Dual UNION ALLSELECT 21 AS Id, '花生' AS NAME FROM Dual UNION ALLSELECT 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, '')) Name3FROM TestGROUP BY Trunc(Rownum / 3 - 0.1, 0)ORDER BY Id1

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表