Skip to content

Rrecursive query issue on Redshift

0

I permanently got an error: Recursive CTE must not omit column names 1-6765d9b8-015811ca424804b42777bcd2 even when I run simple query: WITH RECURSIVE numbers AS ( SELECT 1 AS n UNION ALL SELECT n + 1 -- here doesn't work with and without alias FROM numbers WHERE n < 10 ) SELECT n FROM numbers;

env: redshift serverless editor: AWS Query Editor v2 version: PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red Hat 3.4.2-6.fc3), Redshift 1.0.80583

working with data type date also doesn't work.

asked 2 years ago1.2K views

1 Answer
2

The issue you're experiencing with recursive queries in Amazon Redshift is likely due to the specific syntax requirements for recursive Common Table Expressions (CTEs) in Redshift.

In Redshift, when using recursive CTEs, you must explicitly name all columns in the CTE definition. This is different from some other database systems where column names can be omitted.

To resolve your error, try modifying your query to explicitly name the column in the recursive part of the CTE. Here's a corrected version of your query:

WITH RECURSIVE numbers(n) AS ( SELECT 1 AS n UNION ALL SELECT n + 1 AS n FROM numbers WHERE n < 10 ) SELECT n FROM numbers;

Note the addition of (n) after the CTE name "numbers" in the first line. This explicitly names the column in the CTE.

For working with date types, the same principle applies. You need to explicitly name all columns in the CTE definition.

It's worth noting that while Redshift is based on PostgreSQL, it has some specific differences in syntax and functionality, particularly when it comes to recursive queries. Always refer to the Redshift-specific documentation for the most accurate information on syntax and features.

If you continue to experience issues after making these changes, you may want to check your Redshift version and ensure that recursive CTEs are supported in your current environment.
Sources
Redshift error when using recursive CTE | AWS re:Post
Query troubleshooting in Amazon Redshift Spectrum - Amazon Redshift

answered 2 years ago

EXPERT

reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.