How to use string aggregation on concatenated result subquery in Postgres? - postgresql

I am trying to use string aggregation on the results of a subquery that contains concatenated results. I would like to create a long text string containing the qualified name "schema.table" for a DB schema I have in Postgres. Do you know how to achieve it? Below is the code I have come up with (not working), it says
more than one row returned by a subquery used as an expression.
SELECT string_agg(
(SELECT CONCAT(table_schema, '.', table_name) table_schema_name
FROM information_schema.tables
WHERE table_schema = 'name_of_schema'), ' ')
FROM information_schema.tables
WHERE table_schema = 'name_of_schema'
what I have now is:
| table_schema_name |
name_of_schema.table1
name_of_schema.table2
name_of_schema.table3
What I would like to achieve is:
| agrreagated field |
name_of_schema.table1 name_of_schema.table2 name_of_schema.table3

This query should do the task you're asking for:
SELECT string_agg(CONCAT(table_schema, '.', table_name), ' ')
FROM information_schema.tables
WHERE table_schema = 'schema_name'

You should either use the query as table expression in your outer select:
SELECT string_agg(s.table_schema_name, ' ') FROM (
SELECT CONCAT(table_schema, '.', table_name) AS table_schema_name
FROM information_schema.tables
WHERE table_schema = 'public'
) s
or simply avoid the sub-select:
SELECT string_agg(CONCAT(table_schema, '.', table_name), ' ')
FROM information_schema.tables
WHERE table_schema = 'public';

Related

PostgreSQL how to check table existence?

This is how we can check table existence in MSSQL:
IF OBJECT_ID(N'public."TABLE_NAME"', N'U') IS NOT NULL
select 1 as 'column'
else
select 0 as 'column';
which stores outcome in variable 'column'
How can I do same thing in PostgreSQL ? I want to return 1 or 0 for respective outcome.
Use a SELECT with an EXISTS operator checking e.g. information_schema.tables:
select exists (select *
from information_schema.tables
where table_name = 'table_name'
and table_schema = 'public') as table_exists;
If you can't (or won't) deal with proper boolean values, the simply cast the result to a number (but I have no idea why that should be better):
select exists (select *
from information_schema.tables
where table_name = 'table_name'
and table_schema = 'public')::int as "column";
Note that column is a reserved keyword and thus you need to quote it using double quotes.
Check for column in a table existence use view pg_tables
IF EXISTS ( SELECT attname
FROM pg_attribute
WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'YOURTABLENAME')
AND attname = 'YOURCOLUMNNAME')
THEN
-- do something
END IF;
For my sql use INFORMATION_SCHEMA.COLUMNS
SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
[AND table_schema = 'db_name']
[AND column_name LIKE 'wild']

Postgresql get different schema table columns

I have a query. I'm using a few schemas. I want to get different schema table column names.
select column_name
from information_schema.columns
where table_name='public.combine'
or table_name='kds.2014_new'
or table_name='public.point'
or table_name='spt.point'
When I run this query I get 0 result. How can I solve this problem?
You have to separate table_name and table_schema
SELECT column_name
FROM information_schema.columns
WHERE (table_name = 'combine' AND table_schema = 'public')
OR (table_name = '2016_new' AND table_schema = 'kds')
OR (table_name = 'point' AND table_schema = 'public')
OR (table_name = 'point' AND table_schema = 'spt')

How to add single quotes around a list of columns names?

I have a table which has few columns. I got the name of columns by getting it as an array_agg and then array_to_string. Like below:
"hospitalaccountrecord,locationname,patientkey,inpatientadmitdatetime,readmission,no_null_days_btw_admissions,cohort_assignment,admit_mon_feb,admit_mon_mar,admit_mon_apr,admit_mon_may,admit_mon_june,admit_mon_july,admit_mon_aug,admit_mon_sep,admit_mon_oct,a (...)"
The code I used was this:
select array_to_string(array_agg(column_name::text), ',')
from
(
select column_name
from
information_schema.columns
where table_schema='a' and
table_name = 'b'
order by columns.ordinal_position
) as v;
What I am looking for is the same thing but each column name should be enclosed in ''. Like
'hospitalaccountrecord','locationname','patientkey'
and so on.
Don't need two SELECTs, only
select string_agg(''''|| your_column_name ||'''', ',')
should do the job.
select string_agg(quoted_column_name, ',')
from
(
select '''' || column_name || '''' as quoted_column_name
from
information_schema.columns
where table_schema='a' and
table_name = 'b'
order by columns.ordinal_position
) as v;

SELECT ALL column_names in postgresql

I'm using PostgreSQL and I want to create a query that will display all column_names in a specific table.
Schema: codes
Table Name: watch_list
Here are the column_names in my table:
watch_list_id, watch_name, watch_description
I tried what I found in the web:
SELECT *
FROM information_schema.columns
WHERE table_schema = 'codes'
AND table_name = 'watch_list'
It output is not what I wanted. It should be:
watch_list_id, watch_name, watch_description
How to do this?
If you want all column names in a single row, you need to aggregate those names:
SELECT table_name, string_agg(column_name, ', ' order by ordinal_position) as columns
FROM information_schema.columns
WHERE table_schema = 'codes'
AND table_name = 'watch_list'
GROUP BY table_name;
If you remove the condition on the table name, you get this for all tables in that schema.
SELECT table_name FROM information_schema.tables WHERE table_schema='public'

PostgreSQL: Search all tables of a database for a field called LIKE *active*

In my public schema I have 1200 tables.
Somewhere in one or more of this tables there are some fields called LIKE "active"
like:
- status_active
- hr_active
- who_knows_what_active_could_be
I want to find them all using PGAdmin in the console or via the normal client on console
how could I do this with quick with less resources?
Try:
SELECT *
FROM information_schema.columns
WHERE TRUE
AND table_schema = 'public'
AND column_name ~* 'active'
You can try:
SELECT table_name,tables.table_catalog,tables.table_schema,column_name
FROM information_schema.columns
inner join information_schema.tables
using(table_name,table_schema)
WHERE table_type = 'BASE TABLE'
and column_name ilike '%active%'
select * from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'your schema name'
--and TABLE_NAME ilike '%your keyword%' --when you need to search for a TABLE
and COLUMN_NAME ilike '%your keyword%' --when you need to search for a COLUMN