Query to find the column names of a table using BIgquery - sql

I am working on project_name='Alpha' and dataset_name = 'Beta' and this dataset contains numerous tables. I want to get table details i.e, columns of a particular table i.e, table_name = 'Gamma'

To retrieve meta data information. you can use INFORMATION_SCHEMA.
Try with below query..
SELECT column_name FROM `project_name.dataset_name.INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = <'Table Name'>;

For your solution, You can use INFORMATION_SCHEMA.COLUMNS
First, try with below:
SELECT * FROM Alpha.Beta.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Gamma' ;
Then you can select the details you want, only for the column name you can use below;
SELECT column_name FROM Alpha.Beta.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Gamma' ORDER BY ordinal_position asc ;
Please do let me know if it doesn't work out but flag it as an answer if it works.
EDIT: if it didn't work, can you please try putting Alpha.Beta.INFORMATION_SCHEMA.COLUMNS between escape character

Related

SQL Match table and multiple columns

I wanted to know a Query where in which i want to locate a specific table that contains two or more columns:
I tried:
SELECT *
FROM DB
WHERE TableName = 'TableName'
AND ColumName in('column1' , 'column2')
But this query will look if any of those columns are there, but i want it to return only if all of them are a match.
I hope this questions makes sense.
This should work for you in MySQL:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME in ('column1','column2')
AND TABLE_SCHEMA='your_database'
GROUP BY table_name
HAVING COUNT(COLUMN_NAME) =2;
The operator IN is a concatenation of OR. However, there's no way to creare a short concatenation of AND as well.
See this question.

Get a list of database tables that contain a specific column field?

How to select all tables that contain a specific column?
Is this what you expect?
demo: db<>fiddle
SELECT table_name
FROM information_schema.columns
WHERE column_name = 'your_column_name'

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

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.

How does SELECT 'TABLE_NAME' AS TEMP work?

I found this (working) statement in some code I maintain:
SELECT 'TABLE_NAME' AS TEMP
FROM TABLE_NAME
WHERE ROWNUM = 1
and while I am familiar with the AS use for table columns, e.g.
USE mydatabase;
SELECT day_of_order AS "Date",
customer As "Client",
product,
quantity
FROM orders;
I couldn't find documentation for using the entire table's name as a column.
How does this work? and what would be the intended use of such statement?
It just looks like they're selecting a literal string of the table name, and put it in a column called TEMP. It will only return one row because of the WHERE ROWNUM = 1. They could have just as easily done:
SELECT 'TABLE_NAME' AS TEMP FROM DUAL
As for what you'd use it for... that's a good question. :)