I have two tables (a,b), or joining table to itself, having the same column names. (The table(s) is wide, having about hundred columns.)
Using:
SELECT a.*, b.*
FROM a LEFT JOIN b ON b.id=a.id AND b.date1=a.date2;
... works, but now if I want to make a view from it:
CREATE OR REPLACE VIEW v_test_ab AS
SELECT a.*, b.*
FROM a LEFT JOIN b ON b.id=a.id AND b.date1=a.date2;
... it cannot make a view, because there would be multiple columns with same name.
In PostgreSQL, is there any way to automatically rename the column names of a result table using given pattern?
Example something like:
CREATE OR REPLACE VIEW v_test_ab AS
SELECT a.* AS a_*, b.* AS b_*
FROM a LEFT JOIN b ON b.id=a.id AND b.date1=a.date2;
Or perhaps something like:
SELECT a.* AS generate_column_names('a_%', a.*)
, b.* AS generate_column_names('b_%', b.*)
Avoiding to need to write:
CREATE OR REPLACE VIEW v_test_ab AS
SELECT
a.id as a_id, a.date1 as a_date1, a.date2 as a_date2 -- and so on
, b.id as b_id, b.date1 as b_date1, b.date2 as b_date2 -- and so on
FROM a LEFT JOIN b ON b.id=a.id AND b.date1=a.date2;
I can of course use pattern matching search & replace in IDE-editor, but an end result looks like bloated. (The real world case I have, has four wide source tables with many same column names.)