Search code examples
sqlpostgresqlmetabase

PostgreSQL: with clause returning syntax error at end of input


I'm trying to create a temporary table with the following code:

with cte_counts as 
(select entity_id, entity_name, count(distinct segment_id) as countries
from cte_geography
where cte_geography.metric_id in (2, 20, 35)
group by 1, 2
order by 3 desc)

select *
from cte_counts

but I get the error: ERROR: syntax error at end of input Position: 529

I've retyped it and I can't spot what's wrong. Without with, the code works fine:

select entity_id, entity_name, count(distinct segment_id)
from cte_geography
where cte_geography.metric_id in (2, 20, 35)
group by 1, 2
order by 3 desc

NOTE: performing this on Metabase.


Solution

  • I had a similar issue when creating a CTE. I was getting a syntax error at the end ). I tried what @AdrianKlaver suggested and added; ) Select * From CTE. It worked perfectly after that.