I have a database and have 2 schema called DWFR and DWFR3 , what i need to do is compare 2 schmea and take the matching table name and also the table present in DWFR schema which is not available on DWFR3 and also with the below condition
SELECT schema_name, table_name
FROM tables
WHERE schema_name = 'DWFR'
AND table_name NOT LIKE 'RA%'
AND table_name NOT LIKE 'ZZ%'
AND table_name NOT LIKE 'DELETE%'
AND table_name NOT LIKE '%TEST%'
AND table_name NOT LIKE 'XX%'
AND table_name NOT LIKE 'ROB%'
AND table_name IN (
SELECT table_name
FROM tables
WHERE schema_name = 'DWFR3'
AND table_name NOT LIKE 'RA%'
AND table_name NOT LIKE 'ZZ%'
AND table_name NOT LIKE 'DELETE%'
AND table_name NOT LIKE '%TEST%'
AND table_name NOT LIKE 'XX%'
AND table_name NOT LIKE 'ROB%'
)
ORDER BY schema_name, table_name;
This will get the matched table names but i also want table name available in DWFR which is not their on DWFR3.
You can use a LEFT OUTER JOIN for selecting all data rows from a set and matchings from a second set.
I used a SQL CTE Common Table Expression in the script, you can think of it as a sub-select with additional uses
Here is how you can use for your case
with t2 as (
select table_name
from tables
where schema_name = 'DWFR3'
)
select t1.table_name, t2.table_name
from tables t1
left outer join t2
on t1.table_name = t2.table_name
where t1.schema_name = 'DWFR'
And you also need the append the additional requirements or WHERE clauses into above script
For the reason that I used CTE can be explained as follows.
In fact, the actual solution should be as simple as following.
Unfortunately, HANA seems to not supporting additional filtering criteria with ON clause on JOINs
select t1.table_name, t2.table_name
from tables t1
left outer join tables t2
on t1.table_name = t2.table_name and t2.schema_name = 'DWFR3
where t1.schema_name = 'DWFR'
Because of this reason, I had to filter the LEFT part of the JOIN earlier than applying the ON, join condition
To find matched table names in both schemas:
select
'DWFR' as schema_name,
table_name
from
(
select table_name from tables where schema_name = 'DWFR'
intersect
select table_name from tables where schema_name = 'DWFR3'
) x
where
table_name not like 'RA%' and
table_name not like 'ZZ%' and
table_name not like 'DELETE%' and
table_name not like '%TEST%' and
table_name not like 'XX%' and
table_name not like 'ROB%';
To find table names in DWFR but not in DWFR3:
select
'DWFR' as schema_name,
table_name
from
(
select table_name from tables where schema_name = 'DWFR'
except
select table_name from tables where schema_name = 'DWFR3'
) x
where
table_name not like 'RA%' and
table_name not like 'ZZ%' and
table_name not like 'DELETE%' and
table_name not like '%TEST%' and
table_name not like 'XX%' and
table_name not like 'ROB%';
Related
I really only want the number of columns in the tables in the SQL query, but somehow i can't get the right result. Does anybody know what I'm doing wrong?
select count(*) from user_tab_columns where table_name='tablename' //works and gives me the number of columns
select
TABLE_NAME,
NUM_ROWS,
(select count(*) from user_tab_columns where table_name=TABLE_NAME) as Test
from user_tables
Haha, look at this:
where table_name=TABLE_NAME
This will always be true, because the table name is the table name.
Here is the query with qualified column names:
select
table_name,
num_rows,
(select count(*) from user_tab_columns tc where tc.table_name = t.table_name) as test
from user_tables t;
As an alternative, you could aggregate the whole query:
select t.table_name
, num_rows
, count(*) as num_columns
from user_tables t
join user_tab_columns c on c.table_name = t.table_name
group by t.table_name, t.num_rows
order by 1;
Most of the tables in the DB have a certain column 'col_abc'.
How can I find the tables that do not contain this column 'col_abc'.
The query
select table_name
from user_tab_columns
where column_name not in ('COL_ABC')
does not return expected result.
Thanks,
Amit
Here is one way:
select table_name from user_tables
minus
select table_name from user_tab_columns
where column_name = 'ABC';
The Below Query will give the expected results. Please modify <ur_Schema_name> as the required schema name
SELECT * FROM ALL_TABLES at
WHERE at.OWNER = <ur_Schema_name>
AND NOT EXISTS (SELECT 1 FROM ALL_TAB_COLUMNS atc
WHERE atc.TABLE_NAME = at.TABLE_NAME
AND atc.OWNER =at.OWNER
AND atc.COLUMN_NAME='COL_ABC'
AND atc.OWNER=<ur_Schema_name>);
If you only need to look in the current user's schema, this is probably the simplest way to write the query:
select table_name
from user_tables
where table_name not in (
select table_name
from user_tab_columns
where column_name = 'COL_ABC'
)
;
This uses in a crucial way the fact that the column table_name in user_tab_columns is constrained not null.
If you need to look across all schemas, you need to use the dba_ or all_ dictionary views, and look for (owner, table_name) pairs instead of just table_name. Left as further exercise for you.
You could also use this solution using LEFT JOIN. It is a kind of NOT EXISTS clause.
select t.*
from user_tables t
left join (
select table_name
from user_tab_columns
where column_name = 'COL_ABC'
) tc
on (t.table_name = tc.table_name)
where tc.table_name is null
;
When I run that query in GBQ I receive this error:
Error running query
Resources exceeded during query execution: Not enough resources for
query planning - too many subqueries or query is too complex.. at
[1:1]
The code below is not developed by me so I have a hard time rewriting it. So would hear if you could help me get it running again.
execute immediate (select concat('create or replace table `ssmdw-208309.temp_piwik_and_ga.005_visitor_union` as ',query) from (select string_agg(CONCAT(' select substr(generate_uuid(),25,12) as rowid, ',columns,' from `piwik-272210.piwik.',table_name,'`'),'\nUnion all\n') query FROM (
SELECT
table_column.table_name,
STRING_AGG(CASE
WHEN x.column_name IS NULL THEN CONCAT('null as ',table_column.column_name)
ELSE
table_column.column_name
END
,', '
ORDER BY
table_column.column_name) columns
FROM (
SELECT
tables.table_name,
columns.column_name
FROM (
SELECT
DISTINCT column_name
FROM
`piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` a where table_name like 'visitor_%') columns
FULL OUTER JOIN (
SELECT
DISTINCT table_name
FROM
`piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` b where table_name like 'visitor_%') tables
ON
(1=1)) table_column
LEFT JOIN
`piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` x
ON
(x.table_name = table_column.table_name
AND x.column_name = table_column.column_name)
GROUP BY
1 )))
Avoid "order by" clause wherever it is possible. "Order by" tries to pull result in one node that in general causes this issue.
Try this. then call the procedure.
create or replace procedure `dataset_name'.'procedure_name'()
BEGIN
create temp table TABL1 as (
SELECT DISTINCT column_name FROM `piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` a where table_name like 'visitor_%');
create temp table TABL2 as (
SELECT DISTINCT table_name FROM `piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` b where table_name like 'visitor_%');
create temp table TABL3 as
SELECT tables.table_name, columns.column_name FROM (select column_name from TABL1 columns
FULL OUTER JOIN
select table_name from TABL2 tables ON (1=1));
create or replace table dataset.TABL4 as
SELECT table_column.table_name,
STRING_AGG(CASE WHEN x.column_name IS NULL THEN CONCAT('null as ',table_column.column_name) ELSE table_column.column_name END) as columns
FROM (
SELECT table_name, column_name FROM TABL3
) table_column
left join `piwik-272210.piwik.INFORMATION_SCHEMA.COLUMNS` x
ON (x.table_name = table_column.table_name AND x.column_name = table_column.column_name)
))
drop table TABL1 ;
drop table TABL2 ;
drop table TABL3;
END;
I want to find all tables in my database that have two columns LOCATION and ASSET_ID
So I tried this :
select owner, table_name, column_name
from all_tab_columns
where column_name in ('LOCATION','ASSET_ID');
the problem is this query give all tables that have LOCATION or ASSET_ID not both of them.
so I changed it to this :
select owner, table_name, column_name
from all_tab_columns
where 1=1
and column_name ='LOCATION'
and column_name = 'ASSET_ID';
it shows 0 result.
Please help.
Select all the rows as in your initial attempt. Then group by owner and table_name and keep only those that have TWO rows returned in the initial query. Use the having clause for that:
select owner, table_name
from all_tab_columns
where column_name in ('LOCATION','ASSET_ID')
group by owner, table_name
having count(*) = 2
;
You can do this using a couple of subqueries factored as common table expressions and a join, as in:
WITH cteLOCATION AS (SELECT OWNER, TABLE_NAME
FROM ALL_TAB_COLS
WHERE COLUMN_NAME = 'LOCATION'),
cteASSET_ID AS (SELECT OWNER, TABLE_NAME
FROM ALL_TAB_COLS
WHERE COLUMN_NAME = 'ASSET_ID')
SELECT OWNER, TABLE_NAME
FROM cteLOCATION
NATURAL JOIN cteASSET_ID
This is one of the very few situations where I'd ever use NATURAL JOIN.
SQLFiddle here
Best of luck.
Using joins (similar to #BobJarvis' https://stackoverflow.com/a/47984132/754550 but more traditional without with - clause):
select a1.owner,a1.table_name
from all_tab_columns a1,
all_tab_columns a2
where a1.owner=a2.owner
and a1.table_name=a2.table_name
and a1.column_name='LOCATION'
and a2.column_name='ASSET_ID'
order by a1.owner,a1.table_name
or using sets:
select owner, table_name
from all_tab_columns
where column_name='LOCATION'
intersect
select owner, table_name
from all_tab_columns
where column_name='ASSET_ID'
order by owner, table_name
or using 'group by' clause as already posted here https://stackoverflow.com/a/47981174/754550 by #mathguy
select owner, table_name
from all_tab_columns
where column_name in ('LOCATION', 'ASSET_ID')
group by owner, table_name
having count(*)=2
order by owner, table_name
try this,
SELECT *
FROM (SELECT a.*,
COUNT(1) OVER (PARTITION BY table_name) cnt
FROM all_tab_columns a
WHERE column_name IN ('LOCATION','ASSET_ID')
ORDER BY table_name)
WHERE cnt = 2
I'm trying to get make a query to return a list of the column names from a query result in postgresql
e.g.
SELECT column_name
FROM (
SELECT table1.*
FROM table1
LEFT JOIN table2
ON table1.id = table2.tbl1_id
)
is this possible
I DO NOT WANT THE COLUMNS FROM A SINGULAR TABLE!!! so please dont tell me to do
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'table1'
I don't know why you would ever need to do this, but it is possible using some of the JSON functions introduced in 9.3.
SELECT json_object_keys(row_to_json(t)) FROM
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1) t;
This will give you the name of every column returned for a single row. Without the LIMIT you would get the columns repeated for every row returned. If you wanted to see the values returned as well you can get more complex:
WITH t as
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1_id LIMIT 1)
SELECT json_data.key, json_data.value
FROM t, json_each_text(row_to_json(t)) AS json_data;
Both these queries will return all the columns even if they are named the same. If all you want is a list of unique column names, you can utilize hstore:
CREATE EXTENSION hstore; --Create the extension if you need it.
SELECT akeys(hstore(t)) as array_of_columns
FROM
(SELECT * FROM table1
LEFT JOIN table2 ON table1.id = table2.tbl1id LIMIT 1) t;
Maybe there is a less hacky way, but I think this should work:
create table query_column_sample from (your query where 1=2);
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'query_column_sample'
Querying tables returns data. Querying the information schema returns metadata. Column names are metadata.
SELECT 'table1', column_name
FROM information_schema.columns
WHERE table_catalog = 'database_name'
AND table_schema = 'schema_name'
AND table_name = 'table1'
UNION ALL
SELECT 'table2', column_name
FROM information_schema.columns
WHERE table_catalog = 'database_name'
AND table_schema = 'schema_name'
AND table_name = 'table2';
"schema_name" is probably public. But be warned that you can have multiple tables with the same name, as long as they are in different schemas. (You could have "public.table1" and "private.table1".) You can also have multiple tables with the same name in different databases. You need to specify all three values--database name, schema name, table name--to be sure of getting the right data.
If I had to do this in production I'd probably output more data. I'd also probably include the column "ordinal_position", and sort on it. Sorting by "ordinal_position" will order column names as they appear in the current table definition.
SELECT table_catalog
, table_schema
, table_name
, column_name
, ordinal_position
FROM information_schema.columns
WHERE table_catalog = 'sandbox'
AND table_schema = 'public'
AND table_name IN ('table1', 'table2')
ORDER BY table_catalog
, table_schema
, table_name
, ordinal_position;
The only reasonable approach I can come up with is to do something like this. Schema location plays into this, but I'm just posting the basic idea. SQL doesn't have reflection at run time unless you go and hit meta data tables, which is what you seem to be trying to achieve here.
SELECT table1.*
INTO x
FROM table1
LEFT JOIN table2
ON table1.id = table2.tbl1_id LIMIT 0;
select *
from information_schema.columns
where table_Name = 'x';
Also interesting enough while traveling the Internets over lunch I ran into this. Works with PostgreSQL 8.2.0 or higher.
https://github.com/theory/colnames/tree/master/test/sql