Converting one to many relation into a json column in PostgreSQL - sql

I'm trying to query two information_schema tables in PostgreSQL - tables and columns in order to get the following result:
table_name - columns_as_json_array
Sort of converting this one to many relation into a json array column.
I tried a lot of different methods and came up with somethings like this:
SELECT t.table_name, c.json_columns
FROM information_schema.TABLES t
LEFT JOIN LATERAL(
SELECT table_name, json_agg(row_to_json(tbc)) AS json_columns
FROM information_schema.COLUMNS tbc
WHERE t.table_name = tbc.table_name
GROUP BY table_name
) as c ON TRUE;
This results a list of table_names but the json_columns always contains all of the columns available instead of the columns of that certain table.
Any ideas?

I don't really see the point for a lateral join here. As far as concerns, you can get the expected results by aggregating information_schema.columns:
select table_name, json_agg(row_to_json(c)) json_columns
from information_schema.columns c
group by table_name
order by table_name

Related

Adding filters to SQL query (ORACLE database)

I need to list tables, columns and their data type; I used this code:
select TABLE_NAME, COLUMN_NAME, DATA_TYPE
from dba_tab_columns
order by TABLE_NAME;
The problem is that the database is too large and I only need 2000 specific table/column/datatype. Knowing that a table with the same name and column can be present in multiple SCHEMA
For example (I have 2000 lines like that table names are very random and have nothing in common so is the case for the column names):
TABLE Column SCHEMA
------------------------------------------
DMT_AAAAAAA C1111 ANT_A1
DMT_AAAAAAA C1111 ANT_A2
BBBBBBBB A4444 ANT_A3
JHD6365 H5525 ZUGRU
WRK679 C3020 MUSTSU
TDG5378 C66739 SHGUY
I tried to filter by schema using this query :
select TABLE_NAME, COLUMN_NAME, DATA_TYPE
from dba_tab_columns
where OWNER in ('ANT_A1', 'ANT_A2', 'ANT_A1', 'ZUGRU', 'MUSTSU', 'SHGUY')
order by TABLE_NAME;
The issue now is that it lists all columns even the ones I don't need.
Is there a way to filter out only the lines needed?
Expected output :
|table name | column | data type|
DMT_AAAAAAA C1111 NUMBER
DMT_AAAAAAA C1111 VARCHAR
BBBBBBBB A4444 NUMBER
JHD6365 H5525 VARCHAR
WRK679 C3020 VARCHAR
TDG5378 C66739 VARCHAR
Thank you.
If you want to show the information about certain columns across schemas and tables, specify them in an IN clause using tuples:
select owner, table_name, column_name, data_type
from dba_tab_columns
where (owner, table_name, column_name) in
(
('DMT_AAAAAAA', 'C1111', 'ANT_A1'),
('DMT_AAAAAAA', 'C1111', 'ANT_A2'),
('BBBBBBBB', 'A4444', 'ANT_A3'),
('JHD6365', 'H5525', 'ZUGRU'),
('WRK679', 'C3020', 'MUSTSU'),
('TDG5378', 'C66739', 'SHGUY')
)
order by owner, table_name, column_name;
Check whether the following is enough. (I'm considering your output only)
select Distinct TABLE_NAME, COLUMN_NAME, DATA_TYPE from dba_tab_columns
If you don't want the tables, 'BBBBBBBB' and 'WRK679'
Put those in to a table (say tblExclude) and you can use the following
select Distinct TABLE_NAME, COLUMN_NAME, DATA_TYPE from dba_tab_columns a
Left Join tblExclude b on a.TABLE_NAME = b.TABLE_NAME
Where b.TABLE_NAME is null

Query to find column not showing anything?

I'm trying to find a table in a schema with a specific column name. So I used the following script, but it doesn't return anything:
select a.table_name, column_name,DATA_TYPE,DATA_LENGTH
from all_tab_columns a,USER_ALL_TABLES u
where a.TABLE_NAME=u.TABLE_NAME
and column_name like '%LATLONG%'
order by DATA_LENGTH desc;
On the other hand, a SELECT of table LATLONG_DETAIL will display a column called LATLONG_TYPE.
So why isn't the query displaying this table in its result?
All these queries are being run in the schema where table LATLONG_DETAIL resides.
Thanks.
You say you own the table LATLONG_DETAIL. The only other thing I can think of why your query isn't returning anything is that the column name is not in upper case. Does this query return anything?
SELECT a.table_name, column_name,DATA_TYPE,DATA_LENGTH
FROM all_tables u JOIN all_tab_columns a
ON u.table_name = a.table_name
AND u.owner = a.owner
WHERE UPPER(column_name) LIKE '%LATLONG%';

Vertica Dynamic Max Timestamp from all Tables in a Schema

System is HP VERTICA 7.1
I am trying to create a SQL query which will dynamically find all particular tables in a specific schema that have a Timestamp column named DWH_CREATE_TIMESTAMP from system tables. (I have completed this part successfully)
Then, pass this list of tables to an outer query or some kind of looping statement which will select the MAX(DWH_CREATE_TIMESTAMP) and TABLE_NAME from all the tables in the list (200+) and union all the results together into one list.
The expected output is a 2 column table with all said tables with that TS field and the max of each value. Tables are constantly being created and dropped, so the point is to make everything totally dynamic where no TABLE_NAME values are ever hard-coded.
Any idea of Vertica specific ways to accomplish this without UDF's would be greatly appreciated.
Inner Query (working):
select distinct(table_name)
from columns
where column_name = 'DWH_CREATE_TIMESTAMP'
and table_name in (select DISTINCT(table_name) from all_tables where schema_name = 'PTG_DWH')
Outer Query (attempted - not working):
SELECT Max(DWH_CREATE_DATE) from
WITH table_name AS (
select distinct(table_name)
from columns
where column_name = 'DWH_CREATE_DATE' and table_name in (select DISTINCT(table_name) from all_tables where schema_name = 'PTG_DWH'))
SELECT MAX(DWH_CREATE_DATE)
FROM table_name
Thanks!!!
No way to do that in one SQL .
You can used the below method for node max timestamp columns values
select projections.anchor_table_name,vs_ros.colname,max(max_value) from vs_ros,vs_ros_min_max_values,storage_containers,projections where vs_ros.colname ilike 'timestamp'
and vs_ros.salstorageid=storage_containers.sal_storage_id
and vs_ros_min_max_values.rosid=vs_ros.rosid
and storage_containers.projection_name=projections.projection_name
group by projections.anchor_table_name,vs_ros.colname

How to determine the number of "Column Name" in a table?

I have a table tblEmployeeInfowhich has atleast a 100+ column name.
I want to know how many column name are in that table. Is that possible?
NOTE:
tbleEmployeeInfo has no data inside yet.
I would recommend using the INFORMATION_SCHEMA views. You can see all the columns and their types by doing:
select c.*
from INFORMATION_SCHEMA.COLUMNS c
where table_name = 'tbleEmployeeInfo';
(You might want to include the table_schema as well.)
To get the count, just use COUNT(*):
select count(*)
from INFORMATION_SCHEMA.COLUMNS c
where table_name = 'tbleEmployeeInfo';
SELECT COUNT(*)
FROM sys.columns
WHERE object_id = object_id('tblEmployeeInfo')

Find table_name that contains two known column names

I want to find the tables that contain both of the two columns together in one table. I tried this:
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('CurrencyName', 'CurrencyKey');
This query generates all the tables that has either one of the CurrencyName or CurrencyKey column.
But I want the table that has both of these columns together.
Please shoot some ideas.
Thanks!
You are close. You want to use group by and then validate that you have two matches using a having clause:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('CurrencyName', 'CurrencyKey')
GROUP BY TABLE_NAME
HAVING COUNT(*) = 2;
Note: To be sure you have the right table, you should use TABLE_SCHEMA as well in the query.