Skip to content
Advertisement

Window function is not allowed in where clause redshift

I have a dates CTE in my below query where I am using limit clause which I don’t want to use it. I am trying to understand on how to rewrite my dates CTE so that I can avoid using limit 8 query.

Below is the output I get from my inner dates CTE query:

Output from dates CTE :

Is there any way to avoid using limit 8 in my CTE query and still get same output? Our platform doesn’t allow us to run queries if it has limit clause in it so trying to see if I can rewrite it differently in sql redshift?

If I modify my dates CTE query like this, then it gives me error as window function is not allowed in where clause.

Update

Something like this you mean?

Advertisement

Answer

Just move your WHERE clause to the outer SELECT. Seqnum doesn’t exists until the CTE runs but does exist when the result of the CTE is consumed.

UPDATE …

After moving the where clause AndyP got a correlated subquery error coming from a WHERE clause not included in the posted query. As shown in this somewhat modified query:

This was an interesting replacement of a correlated query with a join due to the inequality in the correlated sub query. We thought others might be helped by posting the final solution. This works:

Needing to make a data segment that had all the possible Max(version) for all possible week_of values was the key. Hopefully having both of these queries posted will help other fix correlated subquery errors.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement