How to select a column from the output of 'SHOW COLUMNS' sql - sql

I am using the sql command SHOW COLUMNS in this way
SHOW COLUMNS FROM TABLE A;
It outputs multiple columns, specifically one called 'COLUMN_NAME' that I would like to select.
I tried doing
SELECT COLUMN_NAME FROM (SHOW COLUMNS FROM TABLE A);
which gives an error, is there another way I can just show one of the columns of the output? Is this because the output is not a table so I cannot query from it?

You can use the like function:
show columns like 'COLUMN_NAME' in table A;
There are limitations on using the output of the show command, so an alternate approach is to use the INFORMATION_SCHEMA.
select * from INFORMATION_SCHEMA.COLUMNS where
TABLE_SCHEMA = 'MY_SCHEMA'
and TABLE_NAME = 'A'
and COLUMN_NAME = 'COLUMN_NAME';
If you need to query the results of a show command, you can use the result_scan table function:
show columns like '%' in table t1;
select * from table(result_scan(last_query_id())) where "column_name" = 'COLUMN_NAME';
Remember to double quote the column names produced by the show command. It's a metadata result and the column names are lower case.

Related

How to select all columns not containing a certain substring in Postgresql?

If I have a table with column names like that:
name,
name_raw,
item,
item_raw,
message,
message_raw
...
... etc.
How can I select every column that does not end with _raw dynamically?
Is something like this:
SELECT
[SOME REGEX LOGIC HERE]
FROM
mytable
or similar possible?
If you have so many columns that you cannot write a (static) query, you probably have to change the schema of your db.
If you cannot change the schema, here's a quick and dirty solution that utilizes postgresql's JSON capabilities. It does not return a traditional table with multiple columns, instead it returns a single column that contains json objects which contain all the columns from the original table whose name ends with _raw.
SELECT (
SELECT json_object_agg(key,value)
FROM json_each(to_json(t))
WHERE key ~ 'raw$'
) FROM mytable;
The idea is convert every row from mytable to a JSON object and then use JSON functions to filter the members of the object.
Use the information schema e.g.
SELECT column_name
FROM information_schema.columns
WHERE table_schema = 'my_schema'
AND table_name = 'mytable'
AND NOT column_name LIKE '%\_raw'
note because the underscore itself is a query wildcard it needs to be "escaped"

Select the first 20 columns

Is it possible to select the first 20 columns from my table, without naming out each column in the select?
The columns are always ordered in the same way when I do a select, so there must be some underlying order.
The below query forms the SQL for you. It uses the dictionary table all_tab_columns to fetch the column names for the table.
SELECT ' SELECT '
|| REPLACE(LISTAGG(column_name,',') WITHIN GROUP( ORDER BY column_id),',',','
||CHR(10))
|| ' FROM YOUR_TABLE'
FROM all_tab_columns
WHERE owner ='YOUR_SCHEMA_NAME'
AND table_name='YOUR_TABLE_NAME'
AND column_id <= 20;
you can use column index instead of column name like select 0,1,2,.....
There is a table in a SQL Server database called sysColumns that records all the columns in every table. I think it is a SQL standard and should be in Oracle too.
EDIT: thanks to comment from #davegreen100, this table is in Oracle but is named DBA_TAB_COLUMNS.
try running Select * from DBA_TAB_COLUMNS and see what the results are, and work from there.
If it's there (in Oracle), you will eventually end up with something like
Select name from DBA_TAB_COLUMNS
Where id = #tableId -- <--- the id of the table
and colOrder <= 20
Your final SQL will be probably have to be generated dynamically using the output from the above

Select columns containing string

Is it possible to retrieve all data in columns containing a particular string? I do not have any control over the data structure and I rather shorten my code than to type everything if possible. As far as I know statements as LIKE and CONTAIN are only used to retrieve particular rows instead of columns. Lets say I have a table with column names: time, run1, run2, run3, ....., run 34, I would like to use something like:
SELECT columns FROM [Tablename] WHERE columns CONTAIN 'run';
Is this possible?
Thanks!
Use like.
In Oracle, you could do
SELECT column_name
from all_tab_columns
where table_name = 'YOUR_TABLE'
and lower(column_name) like 'run%'
In SQL Server, you could do
SELECT column_name
from information_schema.columns
where table_name = 'YOUR_TABLE_NAME'
and lower(column_name) like 'run%'

Query to get columns from system.catalog table and do a select query

I am trying to build a query to fetch columns from the system.CATALOG table and to continue querying based on the resultset. I looked at a few queries but seem to be unable to find anything that satisfies my requirements. I don't have much to show, that I have tried as I don't know, how to approach this.
I am using Apache Phoenix DB. (Any SQL is also OK, as I am interested in learning.)
I have now written the query below, which will fetch me all the column names, that start with A in schema test for table element.
SELECT
COLUMN_NAME
FROM SYSTEM.CATALOG
WHERE TABLE_SCHEM = 'TEST'
AND TABLE_NAME = 'ELEMENT'
AND COLUMN_NAME LIKE 'A%'";
Now I want to use the list of columns names in an UPSERT query from the resultset of above query, to update these columns in the element table's records. So I am stuck here.
try this, it works perfect.
SELECT column_name
FROM system.catalog
WHERE table_name = 'your_table' AND key_seq IS NOT NULL
Example: To get the salt buckets on the table :
select table_name, salt_buckets
from SYSTEM.CATALOG
where salt_buckets is not null and table_name='TABLE_NAME';

Get column name for a table

I want a query that returns me name of columns of a table.
I tried following query -
SELECT column_name ColName
FROM all_tab_columns
WHERE table_name ='<TABLENAME>';
but this query is returning duplicate column names.
I do not want to use distinct in my query cause it hampers the performance.
So is there a query which returns columns of a table without any duplicate.
This is the solution for getting column name from table.
select column_name,data_type from cols where Table_name='TEST_TABLE'