PostgreSQL
 SQL >> Database >  >> RDS >> PostgreSQL

Creare tabelle temporanee in SQL

Per creare una tabella temporanea con dati puoi usare CREATE TEMP TABLE AS:

CREATE TEMP TABLE temp1 AS
SELECT dataid, register_type, timestamp_localtime, read_value_avg
FROM rawdata.egauge
WHERE register_type LIKE '%gen%'
ORDER BY dataid, timestamp_localtime;

La tabella è una snapshot statica, visibile solo nella sessione corrente. Con ON COMMIT DROP scompare a fine transazione.

Le temp table hanno priorità nel search path. Se vuoi qualcosa di dinamico, usa CREATE VIEW.

CREATE TABLE LIKE (...), invece, copia solo la struttura, non i dati.