Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create and call temp table in redshift

I want to create a temp table and call this temp table in redshift? I have tried this one but got an error message.

select  top 10 * into #a from public.item_activity where item_act_timestamp < DATEADD(day,-1,getdate());

select * from #a

"ERROR: 42P01: relation "#a" does not exist"

like image 918
user3769827 Avatar asked Apr 20 '15 04:04

user3769827


People also ask

How do you drop a temporary table in Redshift?

Temp tables are automatically dropped at the end of the session in which it is created. Agreed with above comment, what is your use case? are you "cleaning up"? you can delete temp tables only within the current session that they are created though!

How do I create a temporary table in redshift?

The temporary table is created in a separate, session-specific Redshift schema. However, you cannot specify a name for this schema. The schema is internal and not visible to the user. To create a temporary table, simply specify the TEMPORARY keyword (or TEMP abbreviation) in your CREATE TABLE DDL statement.

What are temp tables in Amazon Redshift?

Similar to many other relational databases such as Netezza, Snowflake, Oracle, etc. Amazon Redshift support creating temp or temporary tables to hold non-permanent data. i.e. Data which you will use only in the current session and Redshift will drop the temp table soon after the session ends.

Can I use UTF-8 multibyte characters in a redshift query?

You can use UTF-8 multibyte characters up to a maximum of four bytes. Amazon Redshift enforces a quota of the number of tables per cluster by node type, including user-defined temporary tables and temporary tables created by Amazon Redshift during query processing or system maintenance.

How is data stored in a redshift table?

Unlike relational databases, data in a Redshift table is stored in sorted order. The order of sort is determined by setting one or more columns in a table as the sort key. This architecture helps improve SQL query performance in Redshift. A Redshift Sort Key (SORTKEY) can be set at the column level, or at the table level.


3 Answers

You can use

Create temporary table tbl_name as (Select * from abc)
--OR
create temporary table tmp_date_var AS
( SELECT '2017-04-01'::Date AS stdt,'2018-01-31'::Date AS enddt);
like image 66
Ram Avatar answered Oct 25 '22 06:10

Ram


Please create table table using command below below:

  CREATE TEMP TABLE temp_table_name ....

Once this is done , use select query as below

  select * from temp_table_name

The temp table exists only for duration of your session using which you have created the table.

like image 10
Sandesh Deshmane Avatar answered Oct 25 '22 07:10

Sandesh Deshmane


This problem vexed me for a while as well so posting solution even though the question was asked a while ago. The solution lies in the tool you're using, Aginity in my case, there are 2 ways to solve:

  • Temporary solution for one query window and one query session: Click on Options-> Current Query Options -> Check box "Keep connection open between executions"
  • More permanent solution for all query windows and all sessions: Click on Tools -> Options -> Query Analyzer -> Select "Keep connection open between executions" under Connection Handling

If you're not using Aginity, look for a similar setting in your tool.

Hopefully, I was able to help someone with this!

like image 4
Nisheeth Singh Avatar answered Oct 25 '22 08:10

Nisheeth Singh