SELECT INTO syntax for Snowflake Datawarehouse
Asked Answered
A

3

20

I believe there's an SELECT INTO-like syntax in Snowflake, but I am unable to find documentation or examples to use it.

CREATE TABLE raw_data (
    Timestamp TIMESTAMP NOT NULL, 
    Date DATE NOT NULL, 
    UserID STRING,
    Address STRING,
    Phone STRING,
    Value INTEGER
);

COPY INTO raw_data from 's3://my_bucket'
CREDENTIALS=(AWS_KEY_ID='XXXXX' AWS_SECRET_KEY='XXXX')
ON_ERROR=CONTINUE;

CREATE TABLE summary (
    Date DATE NOT NULL,
    UserID STRING,
    Value INTEGER
);

INSERT INTO summary 
SELECT Date, UserID, Value FROM raw_data

The above works, but rather than defining the table summary I want to SELECT INTO and have the SQL parser create the table for me.

Asylum answered 8/11, 2015 at 21:26 Comment(3)
create table summary as select ... would be standard SQLRealtor
Wow. That's a really documentation-free website they've got there, isn't it?Jibe
@MattGibson There's documentation inside the tool. I think they are only recently beginning to open up to a larger customer base.Asylum
N
31

You can use Create Table AS (CTAS) to accomplish this. In your case it would be:

CREATE TABLE SUMMARY AS
SELECT
  Date
, UserID
, Value
FROM
  raw_data;

Here is an example using no data that I have tested:

create table foo as select $1, $2
from

    values ( 1, 'two' ), ( 3, 'four' ), ( 5, 'six' );

    select * from foo;

Hope this helps!

Nassi answered 28/1, 2016 at 18:21 Comment(0)
C
1

CREATE TABLE summary AS SELECT Date, UserID, Value FROM raw_data

Crofoot answered 24/4, 2020 at 16:58 Comment(0)
G
-4

You can also use the standard "WITH AS" nomenclature:

    with a as (
        SELECT
        Date
        ,UserID
        ,Value
        FROM
        raw_data)
    SELECT * FROM a
Graehme answered 10/9, 2019 at 15:31 Comment(2)
Will give you a CTE.Nordstrom
You are right...now I see why people are voting it down :)Graehme

© 2022 - 2024 — McMap. All rights reserved.