Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to create a CTE with values in postgres?

Tags:

sql

postgresql

I often just want to test out a function, or see what a variable might look like. This could be done by creating a temporary table, but I suspect that there is an easier way.

Basically,

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

This would return

temp_var1 temp_var2 temp_var3
1 4 5
2 5 7
3 6 9

Darn she's fine.

Note I'm using PostgreSQL 9.2.15.

like image 381
edog429 Avatar asked Sep 12 '25 16:09

edog429


1 Answers

I think you are looking for

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

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!