有没有办法用 postgres 中的值创建 CTE?

edo*_*429 17 sql postgresql

我经常只是想测试一个函数,或者看看变量可能是什么样子。这可以通过创建临时表来完成,但我怀疑有一种更简单的方法。

基本上,

WITH cte1 AS (
   SELECT 
      VALUES(1,2,3) AS temp_var1
    , VALUES(4,5,6) AS temp_var2
)
SELECT 
    temp_var1
  , temp_var2
  , (temp_var1 + temp_var2) AS temp_var3
FROM cte1
Run Code Online (Sandbox Code Playgroud)

这将返回

临时变量1 临时变量2 临时变量3
1 4 5
2 5 7
3 6 9

该死,她很好。

注意我使用的是 PostgreSQL 9.2.15。

小智 33

我想你正在寻找

WITH cte1 (temp_var1, temp_var2) AS (
  VALUES
   (1,4),
   (2,5),
   (3,6)
)
SELECT 
    temp_var1
  , temp_var2
  , (temp_var1 + temp_var2) AS temp_var3
FROM cte1
Run Code Online (Sandbox Code Playgroud)