UNION ALL的替代选项

时间:2016-05-24 17:30:59

标签: sql db2 union union-all

我有以下SQL:

SELECT '1.1' AS column_a, '1' as column_b, 4 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '1.2' AS column_a, '1' as column_b, 4001 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.1' AS column_a, '2' as column_b, 1 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.1' AS column_a, '2' as column_b, 2 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.1' AS column_a, '2' as column_b, 3 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.2' AS column_a, '2' as column_b, 1001 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.2' AS column_a, '2' as column_b, 1002 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.2' AS column_a, '2' as column_b, 1003 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.2' AS column_a, '2' as column_b, 1004 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.2' AS column_a, '2' as column_b, 1005 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.2' AS column_a, '2' as column_b, 1006 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.2' AS column_a, '2' as column_b, 2001 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.2' AS column_a, '2' as column_b, 2002 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.2' AS column_a, '2' as column_b, 2003 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '2.2' AS column_a, '2' as column_b, 2004 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '3.1' AS column_a, '3' as column_b, 3001 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '3.1' AS column_a, '3' as column_b, 3002 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '3.2' AS column_a, '3' as column_b, 3003 AS column_c FROM SYSIBM.SYSDUMMY1
UNION ALL
SELECT '3.2' AS column_a, '3' as column_b, 3004 AS column_c FROM SYSIBM.SYSDUMMY1

SQL结果是固定的,所有值都是相同的。

我正在使用DB2。如果没有SQL中的那么多重复,有没有更好的方法来做同样的事情?

2 个答案:

答案 0 :(得分:1)

正如Lashane评论的那样,如果您的平台和DB2版本支持它,VALUES可能就是答案。

这适用于DB2 for i v7.1

#selector(YourClass.someAction)

答案 1 :(得分:1)

您可以使用PureXML将嵌入的XML块解析为表格:

SELECT * 
FROM XMLTABLE (
'/set/row'
PASSING XMLPARSE(
'<set>
<row a="1.1" b="1" c="4"/>
<row a="1.2" b="1" c="4001"/>
<row a="2.1" b="2" c="1"/>
</set>')
COLUMNS
column_a CHAR(3) PATH '@a',
column_b CHAR(1) PATH '@b',
column_c INTEGER PATH '@c'
) AS X