Avatar of ttist25
ttist25

asked on 

Redshift INSERT INTO SELECT DISTINCT FROM cte

Hopefully that title explains it well enough but...

I'm working in a Redshift instance and I have a CTE that pulls fields from a couple tables and uses dense_rank to add an id for the grouped rows.  It seems to be working fine because I can select rows from my CTE and the results appear as I would expect.  

Next, I'm trying to insert the distinct rows from that CTE into an existing table.  That's where all hell breaks loose.  Well, maybe not all hell but definitely an error. :)

I'm still a noob w/ Redshift so I'm not sure how to do temp tables or I'd give some DDL here but, here is essentially what the code looks like:

WITH cte_MyCTE AS (
    SELECT 
        tbl1.[MyPID],
        tbl1.[MyDate],
        tbl2.[MyNumber],
        tbl2.[MyOtherNumber],
        DENSE_RANK() OVER(ORDER BY tbl1.[MyPID], tbl1.[MyNumber], DATEADD(HOUR, (DATEDIFF(HOUR, '1900-01-01 00:00:00.000', tbl1.[MyDate]/3)*3, '1900-01-01 00:00:00.000')) AS [MyGrpID]
FROM Table1 AS tbl1
LEFT JOIN Table2 AS tbl2
)
INSERT INTO Table3
SELECT DISTINCT
    [MyGrpID],
    [MyPID],
    [MyNumber],
    [MyOtherNumber]
FROM cte_MyCTE

Open in new window



Redshift says
SQL Error [500310] [421601]: [Amazon] (500310) Invalid operation: syntax error at or near "INSERT"

I'm stuck.  any ideas would be greatly appreciated!
SQL

Avatar of undefined
Last Comment
ttist25
ASKER CERTIFIED SOLUTION
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of ttist25
ttist25

ASKER

THANK YOU!!!!

Not sure why it worked but it definitely did!

Can you point me to something that explains why?  Either way - NICE JOB!
Many database products use that syntax.

I'm not familiar with Redshift but if these are the docs:
https://github.com/awsdocs/amazon-redshift-developer-guide/blob/master/doc_source/r_INSERT_30.md

The syntax shows the INSERT first then 'expression'.  The CTE expression is part of the actual select making it part of the expression.
Avatar of ttist25
ttist25

ASKER

Got it!  Thanks again!
SQL
SQL

SQL (Structured Query Language) is designed to be used in conjunction with relational database products as of a means of working with sets of data. SQL consists of data definition, data manipulation, and procedural elements. Its scope includes data insert, query, update and delete, schema creation and modification, and data access control.

61K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo