我需要用 0 和 1 组合制作一个矩阵:
SELECT 1 AS F1, 0 AS F2 ,0 AS F3,0 AS F4, 0 AS F5 FROM DUAL
UNION ALL
SELECT 0 AS F1, 1 AS F2 ,0 AS F3,0 AS F4, 0 AS F5 FROM DUAL
UNION ALL
SELECT 0 AS F1, 0 AS F2 ,1 AS F3,0 AS F4, 0 AS F5 FROM DUAL
UNION ALL
SELECT 0 AS F1, 0 AS F2 ,0 AS F3,1 AS F4, 0 AS F5 FROM DUAL
UNION ALL
SELECT 0 AS F1, 0 AS F2 ,0 AS F3,0 AS F4, 1 AS F5 FROM DUAL
您是否有一些更棘手或更优雅的代码来实现这一目标?
我在 Oracle 19c
谢谢
您可以让数据库为您完成,而不是自己创建组合:
编辑:误读你的问题
除了交叉连接有 2 行的表外,还可以按多维数据集使用分组:
另外三种方法,一种使用函数
SYS.ODCINUMBERLIST()
并TABLE()
生成 5 行表,第二种使用CONNECT BY
(Oracle 用于递归查询的旧方法),第三种滥用GROUPING SETS
.请注意,
TABLE()
在最近的版本中不需要。在第一个查询中,我们可以只使用:在dbfiddle.uk中测试:
版本 21.3.0.0.0
小提琴