getting column names
Hi
Can somebody please tell me if the below is possible ?
I may not see the forest from the trees
Thanks
Armand
levregdb=# select * from foo1;
audit_id | table_name
----------+------------
6012 | foo2
6013 | foo2
6014 | foo2
select * from foo2;
levregdb=# select * from foo2;
foo2_add_by | foo2_add_date
-------------+---------------
(0 rows)
levregdb=# \d foo2;
Table "csiprev.foo2"
Column | Type | Modifiers
---------------+---------------+-----------
foo2_add_by | character(10) |
foo2_add_date | character(10) |
My intention is to have an output like
6012 | foo2|foo2_add_by|foo2_add_date
6013 | foo2|foo2_add_by|foo2_add_date
6014 | foo2|foo2_add_by|foo2_add_date
select a.audit_id, a.table_name, b[1],b[2]
from
foo1 a,
(select
array(
select
column_name::text from
information_schema.columns
where
table_name='foo2'
and
(
column_name like '%add_by%'
or
column_name like '%add_date%'
)) b) as foo
;
audit_id | table_name | b | b
----------+------------+-------------+---------------
6012 | foo2 | foo2_add_by | foo2_add_date
6013 | foo2 | foo2_add_by | foo2_add_date
6014 | foo2 | foo2_add_by | foo2_add_date
But if I join back to foo1 like below I get
select a.audit_id, a.table_name, b[1],b[2]
from
foo1 a,
(select
array(
select
column_name::text from
information_schema.columns
where
table_name=a.table_name
and
(
column_name like '%add_by%'
or
column_name like '%add_date%'
)) b) as foo
;
ERROR: invalid reference to FROM-clause entry for table "a"
LINE 10: table_name=a.table_name
^
HINT: There is an entry for table "a", but it cannot be referenced from this part of the query.
Any idea what am I doing wrong ?
Can I do it ?
Thanks
Armand
--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general
On Tue, Apr 4, 2017 at 2:51 PM, Armand Pirvu (home) <armand.pirvu@gmail.com>
wrote:
But if I join back to foo1 like below I get
select a.audit_id, a.table_name, b[1],b[2]
from
foo1 a,
(select
array(
select
column_name::text from
information_schema.columns
where
table_name=a.table_name
and
(
column_name like '%add_by%'
or
column_name like '%add_date%'
)) b) as foo
;ERROR: invalid reference to FROM-clause entry for table "a"
LINE 10: table_name=a.table_name
^
HINT: There is an entry for table "a", but it cannot be referenced from
this part of the query.
https://www.postgresql.org/docs/9.6/static/sql-select.html
Adding LATERAL before the second "from_item" should get rid of the error -
whether it results in a working and/or correct query I don't know.
David J.
Thanks David
Worked like a charm and results are correct
Armand
On Apr 4, 2017, at 5:00 PM, David G. Johnston <david.g.johnston@gmail.com> wrote:
Show quoted text
On Tue, Apr 4, 2017 at 2:51 PM, Armand Pirvu (home) <armand.pirvu@gmail.com> wrote:
But if I join back to foo1 like below I get
select a.audit_id, a.table_name, b[1],b[2]
from
foo1 a,
(select
array(
select
column_name::text from
information_schema.columns
where
table_name=a.table_name
and
(
column_name like '%add_by%'
or
column_name like '%add_date%'
)) b) as foo
;ERROR: invalid reference to FROM-clause entry for table "a"
LINE 10: table_name=a.table_name
^
HINT: There is an entry for table "a", but it cannot be referenced from this part of the query.https://www.postgresql.org/docs/9.6/static/sql-select.html
Adding LATERAL before the second "from_item" should get rid of the error - whether it results in a working and/or correct query I don't know.David J.