Need to also select the row/record count using the following query in Oracle SQL. How would I go about doing this?
SELECT DISTINCT t.OWNER AS TABLE_SCHEMA, t.Table_Name, c.COLUMN_NAME, c.DATA_TYPE,
(
SELECT CASE WHEN cons.CONSTRAINT_TYPE = 'P' THEN 'Primary Key' ELSE NULL END
FROM SYS.all_cons_columns cols
INNER JOIN SYS.all_constraints cons ON cons.constraint_name=cols.constraint_name AND cons.constraint_type = 'P'
WHERE cols.Column_name=c.column_name AND cols.TABLE_NAME=c.TABLE_NAME AND cols.OWNER=c.OWNER
) AS CONSTRAINT_TYPE, c.DATA_PRECISION, c.DATA_SCALE
FROM SYS.ALL_TABLES t
INNER JOIN SYS.all_tab_columns c ON c.TABLE_NAME=t.TABLE_NAME AND c.OWNER=t.OWNER
WHERE t.OWNER = 'MY_SCHEMA_NAME' AND t.Table_Name = 'MY_TABLE_NAME'
If you can live with an estimate number of rows (whose accuracy depends on the freshness of each table's statistics), you can just use column NUM_ROWS from ALL_TABLES:
SELECT
t.OWNER AS TABLE_SCHEMA,
t.Table_Name,
c.COLUMN_NAME,
c.DATA_TYPE,
t.NUM_ROWS, --> here
...
FROM all_tables t
INNER JOIN all_tab_columns c ...
Note that DISTINCT seem superfluous here - your query generates one record per column in the table.
If you really need the exact count, then you can use an inline query - but you need to hardcode the table name:
SELECT
t.OWNER AS TABLE_SCHEMA,
t.Table_Name,
c.COLUMN_NAME,
c.DATA_TYPE,
(SELECT COUNT(*) FROM my_schema_name.my_table_name) no_rows,
...
FROM all_tables t
INNER JOIN all_tab_columns c ...
WHERE t.OWNER = 'MY_SCHEMA_NAME' AND t.Table_Name = 'MY_TABLE_NAME'
Not sure which SQL you are using but you might put the whole result into a temp table
like
INTO temp temp_table_count WITH NO LOG;
SELECT COUNT(*) FROM temp_table_count;
https://www.oracletutorial.com/oracle-basics/oracle-private-temporary-table/ the reference
INSERT INTO ora$ppt_temp_table_count(ADD ALL COLUMNS HERE
COMMASEPARATED)
SELECT col1, col2, col3
FROM source_table
WHERE condition; <-- your query
SELECT COUNT(*) FROM ora$ppt_temp_table_count;
Related
I need help in SQL Server, I want to get the number of identical values from a column, but I only have column names, here is my script
select
column_name, table_name, data_type, d.name, d.description
from
INFORMATION_SCHEMA.COLUMNS
inner join
Dims d on table_name = 'Dim_' + d.name + '_View'
where
column_name = 'ExtCode'
group by
column_name, table_name, data_type, d.name, d.description
having
count(column_name) > 1;
I want to get the number of records in column names that are greater than 1
I think you are trying to do two things with one SQL:
Find all the column names that are repeated across tables
Display each occurrence of it along with the table they occur in. Break this up into two parts.
Start with a list of repeated column names:
Select column_name From INFORMATION_SCHEMA.COLUMNS
Where table_name like 'Dim%View'
Group By column_name Having count(*)>1
Then join that to your query to retain only the columns that appear more than once:
select
Cols.column_name, cols.table_name, cols.data_type, d.name, d.description
from INFORMATION_SCHEMA.COLUMNS cols inner join Dims d
on table_name = 'Dim_' + d.name + '_View'
Inner Join (
Select column_name From INFORMATION_SCHEMA.COLUMNS
Where table_name like 'Dim%View'
Group By column_name Having count(*)>1
) multi On multi.column_name=cols.column_name
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;
There are 2 tables which have 10 columns common in both the tables and 2 columns exist in table 2 but not in table 1. I have a task where I need to get those 2 columns present in Table 2 but not in Table 1.
The condition for the query is:
We must use information_schema.columns table.
Left join must be used on the same table information_schema.columns
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS t2
LEFT JOIN INFORMATION_SCHEMA.COLUMNS t1
ON t2.COLUMN_NAME = t1.COLUMN_NAME
AND t1.TABLE_NAME = 'table1'
WHERE t2.TABLE_NAME = 'table2'
AND t1.COLUMN_NAME IS NULL;
As per your requirement there care multiple solutions to this problem. Just giving a simple one:
Left join the information_schema.columns table with itself on the table_name and column_names and then filter out the columns of 'table1' using where not exists from the joined result. Finally apply another filter on the joined result to take only the column_name which are in 'table2':
select isc1.column_name from information_schema.columns isc1
left join information_schema.columns isc2
on isc1.table_name = isc2.table_name and isc1.column_name = isc2.column_name
where not exists (select column_name
from information_schema.columns isc
where isc.table_name = 'table1')
and isc1.table_name = 'table2';
A simple way is to take all the columns of Table 2 that are not in Table 1.
SELECT c.COLUMN_NAME
FROM information_schema.tables t
join INFORMATION_SCHEMA.COLUMNS c on t.TABLE_NAME = c.TABLE_NAME
WHERE t.TABLE_NAME = 'table2' and c.COLUMN_NAME not in
(
SELECT c.COLUMN_NAME
FROM information_schema.tables t
join INFORMATION_SCHEMA.COLUMNS c on t.TABLE_NAME = c.TABLE_NAME
WHERE t.TABLE_NAME = 'table1'
)
How to get the number of columns and rows for each of my tables in oracle?
So far I know how to display the number of columns for each table:
select table_name, count(*)
from all_tab_columns
where owner = 'user'
group by table_name
SELECT T.owner,
t.table_name,
t.num_rows, --it depends analyze (gather statics etc.) we can't depend it.
To_number(Extractvalue(Xmltype(
dbms_xmlgen.Getxml('select count(*) c from '
||table_name)),
'/ROWSET/ROW/C')) real_count,
(SELECT Count(*)
FROM all_tab_columns C
WHERE C.table_name = T.table_name) COLUMN_COUNT
FROM all_tables t
WHERE OWNER='user' --and T.table_name = 'MYTABLE'
GROUP BY t.owner,
t.table_name,
t.num_rows
Need to also select the total record count given the following query. How would I go about doing this?
SELECT DISTINCT t.Creator AS TABLE_SCHEMA, t.Name AS Table_Name, c.Name AS COLUMN_NAME,
c.COLTYPE AS DATA_TYPE, CASE WHEN c.KEYSEQ = 1 THEN 'Primary Key' ELSE NULL END AS CONSTRAINT_TYPE, NULL AS DATA_PRECISION, NULL AS DATA_SCALE
FROM SYSIBM.SYSTABLES t
INNER JOIN SYSIBM.SYSCOLUMNS c ON c.TBNAME = t.Name
WHERE t.Creator='MY_SCHEMA_NAME' AND t.Name='MY_TABLE_NAME'
Any help would be appreciated, thanks
For any SELECT statement including your one:
SELECT T.*, COUNT(1) OVER() AS ROW_COUNT
FROM
(
SELECT DISTINCT t.Creator AS TABLE_SCHEMA, t.Name AS Table_Name, c.Name AS COLUMN_NAME,
c.COLTYPE AS DATA_TYPE, CASE WHEN c.KEYSEQ = 1 THEN 'Primary Key' ELSE NULL END AS CONSTRAINT_TYPE, NULL AS DATA_PRECISION, NULL AS DATA_SCALE
FROM SYSIBM.SYSTABLES t
INNER JOIN SYSIBM.SYSCOLUMNS c ON c.TBNAME = t.Name
WHERE t.Creator='MY_SCHEMA_NAME' AND t.Name='MY_TABLE_NAME'
) T;
Need to also select the total record count given the following query.
The SELECT DISTINCT seems superfluous. The system tables should not have duplicates. If you are getting duplicates, it is probably because the JOIN conditions are not complete -- you should join on both the table name and creator.
So just use COUNT(*)
SELECT COUNT(*)
FROM SYSIBM.SYSTABLES t INNER JOIN
SYSIBM.SYSCOLUMNS c
ON c.TBNAME = t.Name AND
c.TBCREATOR = t.TBCREATOR
WHERE t.Creator = 'MY_SCHEMA_NAME' AND
t.Name = 'MY_TABLE_NAME';