-1

如何在 Oracle 中创建下表。

+------+------+
| col1 | col2 |
+------+------+
| A    |    1 |
| B    |    2 |
+------+------+

我需要它作为 WITH 子句中的中间表。

4

3 回答 3

2

您可以使用:

with t as (
      select 'A' as col1, 1 as col2 union all
      select 'B' as col1, 2 as col2
     )

然后,您可以t在查询的其余部分中使用。

于 2020-07-25T17:38:55.067 回答
1

当您创建这样的公用表表达式时,您可以在 CTE 定义中命名列,这样可以避免将它们分散在 SELECT 语句中:

WITH cteWork (COL1, COL2) AS
  (SELECT 'A', 1 FROM DUAL UNION ALL
   SELECT 'B', 2 FROM DUAL)
SELECT *
  FROM cteWork

db<>在这里摆弄

于 2020-07-25T19:35:04.390 回答
1

请使用以下查询,

with tbl as 
  (
   select 'A' as col1, 1 as col2 from dual
   UNION
   select 'B' as col1, 2 as col2 from dual
)

select * from tbl;
于 2020-07-25T17:43:05.403 回答