I have two schemas on my PostgreSQL server, and I'm trying to create a pivoted table:
WITH cte AS (
SELECT l.fcid, t.fixture_code, COUNT(f.fixture_type_id) AS count
FROM aim.locations as l
JOIN aim.fixture_instances as f
ON l.id = f.location_id
JOIN aim.fixture_types as t
ON f.fixture_type_id = t.id
GROUP BY l.fcid, t.fixture_code
)
SELECT *
FROM lab.CROSSTAB('SELECT fcid, fixture_code, SUM(count) FROM cte group by 1,2 order by 1,2',
'Select DISTINCT fixture_code from cte order by 1')
AS CT (FCID integer, "fx1" bigint, "fx2" bigint)
ORDER BY fcid;
However, I receive an error:
ERROR: relation "cte" does not exist LINE 1: Select DISTINCT fixture_code from cte order by 1
The query worked when I had only one schema.
I use a CTE so I could create a view with the query.
What's wrong here?