When running the CTE below, it fetchs me the data I want.
WITH d as
( SELECT *
FROM report.get_sa001('2013-01-01'::date, TO_CHAR(NOW(), 'YYYY-MM-DD')::date, 32) ),
a as
( SELECT *
FROM report."Axis_Reference_All"
WHERE ("Axis_Reference_All"."Site", "Reference_internal", "Customer_code") IN
( SELECT d."Site", "Internal_reference", "Customer_code"
FROM d ) )
SELECT *
FROM d
LEFT JOIN a ON d."Site" = a."Site"
AND d."Internal_reference" = a."Reference_internal"
AND d."Customer_code" = a."Customer_code";
However when I'm trying to create a view on it like below
CREATE VIEW toto AS WITH d as
( SELECT *
FROM report.get_sa001('2013-01-01'::date, TO_CHAR(NOW(), 'YYYY-MM-DD')::date, 32) ),
a as
( SELECT *
FROM report."Axis_Reference_All"
WHERE ("Axis_Reference_All"."Site", "Reference_internal", "Customer_code") IN
( SELECT d."Site", "Internal_reference", "Customer_code"
FROM d ) )
SELECT *
FROM d
LEFT JOIN a ON d."Site" = a."Site"
AND d."Internal_reference" = a."Reference_internal"
AND d."Customer_code" = a."Customer_code";
It gives me the following error
ERROR: The column « Site » has been specified more than once
********** Error **********
ERROR: The column « Site » has been specified more than once
WSQL State :42701
I've tried to put the view name before Site
, namely a
or d
or "Axis_Reference_All"
but none of it works.
Questions are the following:
-
Is CTE allowed with view creation on postgres? I guess no but it is unclear.
-
What would be the best way to create a view with my CTE? That CTE has been built that way because it is improving performance.
Best Answer
There are two possible solutions
Do a
USING
join like belowHowever as I'm retrieving datas from 5 years ago, it takes more than 5 minutes if I'm using a
USING
.The best way is to call each columns separately like below and be sure to name the origin of the column, here a for
Site
andCustomer_code
.This will give the required speed in term of data retrieval.
Cheers