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
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;
I can get the number of columns in all the tables in a postgresql database by
SELECT TABLE_SCHEMA, TABLE_NAME, COUNT(*)
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY TABLE_SCHEMA, TABLE_NAME;
I can get the number of rows in a specific table in a database by
SELECT COUNT(*) FROM mytable
How to obtain column and row counts for all table in a database in one query?
Combine your query using a CTE and join it with the one posted on this answer, as #a_horse_with_no_name suggested, e.g.
WITH j AS (
SELECT table_schema, table_name, count(*) AS count_columns
FROM information_schema.columns
GROUP BY table_schema, table_name
)
SELECT
nspname AS schemaname,relname,reltuples,count_columns
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
JOIN j ON j.table_name = relname AND j.table_schema = nspname
WHERE
nspname NOT IN ('pg_catalog', 'information_schema') AND
relkind='r'
ORDER BY reltuples DESC;
You could either enter \d+ in the selected database using psql from the command line or use the SQL statements from here:
https://wiki.postgresql.org/wiki/Disk_Usage
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;
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