deeejay icon

psql - Prepend table name to column name in a view

deeejay | PRO | 07/17/15 06:19:09 PM UTC | 0 ⭐ | 443 👁️ | Never ⏰ | []
PostgreSQL |

360 B

|

None

|

0 👍

/

0 👎

--1. Using psql, paste query
--2. Copy SQL result
--3. Paste new SQL, execute to create view
 
SELECT 'CREATE OR REPLACE VIEW some_schema.some_view AS SELECT ' || 
(SELECT STRING_AGG(column_name || ' AS some_table_' || column_name, ', ')
FROM information_schema.columns
WHERE table_name = 'some_table'
GROUP BY table_name) || 
' FROM some_schema.some_table;';

Comments