Snowflake Information Schema behavior for View tables - snowflake-cloud-data-platform

I am new to snowflake. I am creating a view in snowflake as below
create view TABLENAME_VIEW as select * from test.stage.TABLENAME;
while running a select statement on information_schema.tables, I am getting the row_count of view table as null whereas doing select count(*) on the view tables gives the count.
Can someone please help on this.
TABLE_NAME
ROW_COUNT
TABLE_CATALOG
TABLE_TYPE
TABLENAME
5
TEST
BASE TABLE
TABLENAME_VIEW
NULL
TEST
VIEW
select count(*) from TABLENAME_VIEW;
COUNT(*)
5

Views are not tables (unless it an materializes view). So they have no rows. When you select from the view it becomes a projection of the underlying table/s
This can be seen by creating another view that has a “WHERE false” thus will return 0 to you select count(*) from the will return 0, but it will have no row. OR if your view had “limit 1”. It will have a count of 1, but which one is the magic part.

Related

SQL Server stored procedure error handling with if exists

I have five databases, In that three databases having column Countries and two databases having column Countryrelease..
I am using a cursor, so if I use exists that particular column is throwing an error that column not exists how to handle this one.
Syntax
if exists(select 1 from table where column name='Countries')
select do some operation
else
select do some operation
You want to make use of the meta data within the SQL instance.
This will work for you ...
if (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'YourTableName' AND COLUMN_NAME = 'Countries') = 1
-- The "Countries" column exists
select do some operation
else
select do some operation

Count null columns in each row - SQL

I want to count number of columns that are null or = '' in each row in SQL. And group by Row_ID.
Something like this:
SELECT
Row_ID, COUNT(*) AS 'cnt_blankCol'
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
table_catalog = 'db'
AND table_name = 'tblName'
AND COLUMNS IS NULL OR COLUMNS = ''
GROUP BY
Row_ID
ORDER BY
COUNT(*)
Thank you.
Information_Schema tables are metadata tables that contain information about the database objects them selves, it does not contain the actual data from the tables, and it does not contain data aggregates per object.
This can not be done querying information_schema. Perhaps the Op can update the question and give a scenario of the goal of the question.

Record count of a view and its corresponding table does not match without any joins in view SQL Server

I have a view as below in sql server:
use database2
Go
CREATE VIEW view1
AS
WITH date_cte(datecol)
AS (select getdate())
Select Col1,
Col2,
,....
,[Select datecol from date_cte]
FROM database1.schema1.TABLE
on top a table in different database.
The record count of table as well as view using statement
Select count(1) from database1.schema1.TABLE -- 15487212
Select count(1) from database2.schema2.view1 -- 13324921
Does this problem have any solution?
First of all... you definitely should improve your code:
USE database2
Go
CREATE VIEW schema2.view1
AS
SELECT Col1,
Col2,
,....
,getdate()
FROM database1.schema1.TABLE
Why do you use a CTE just for the date? Another thing, which may cause your error. You create a view without defining your schema. Maybe your view is created but in a different schema? I've added schema2 to your CREATE due to the fact your querying schema2 at the end.
By the way. Your select can be improved too:
Select count(*) from database1.schema1.TABLE -- ??
Select count(*) from database2.schema2.view1 -- ???
Actually the issue here was that data was loading into source table while I was running query to count records.
Therefore the count difference was prevailing.
Thanks for your input

Create view across multiple databases

I have two databases; 1 is a live database for daily data input and the other is an archival DB for older data.
How can I create a view which gets data from both databases?
Three tables are involve... database1.dbo.table and database1.dbo.tran1 in same database, and database_archived.dbo.table1:
Create VIEW [dbo].[VW_Table_ALL]
AS
SELECT * FROM database1.dbo.table1
UNION ALL
SELECT * FROM database_archived.dbo.table1 as Data INNER JOIN
database1.dbo.tran1 as Tran ON Data.Tran_id = Tran.Tran_Id
GO
Not sure if you need a UNION or a JOIN, but in either case you can just use a three-part name for the object in the other database:
USE database1;
GO
CREATE VIEW dbo.MyView
AS
SELECT columns FROM dbo.LocalTable
UNION ALL
SELECT columns FROM database2.dbo.RemoteTable;
GO

Counting no. of records from multiple tables; Oracle DB

I know i need to use this query to get the list of tables for a schema:
select table_name from all_tables where owner='schema'
I know the following query counts the record in a table:
select count(*) from schema.table
There are 2400+ tables in that schema.
My question is how to count the number of records from all the tables using one step?
You can use DBMS_XMLGEN.GETXMLTYPE function to do this in one shot:
SQL> select table_name
2 , to_number
3 ( extractvalue
4 ( dbms_xmlgen.getxmltype('select count(*) c from ' || table_name)
5 , '/ROWSET/ROW/C'
6 )
7 ) cnt
8 from user_tables
9 order by table_name
10 /
TABLE_NAME CNT
------------------------------ ----------
... [output removed] ...
71 rows selected.
But if your schema contains a lot of data, this might take a long time. Just selecting NUM_ROWS might be sufficient if estimations are ok as well.
Regards,
Rob.
The table ALL_TABLES contains the column NUM_ROWS. (You can get a description of the table with the following SQL statement: DESCRIBE ALL_TABLES;)
The following statement shows the number of records for every table:
SELECT TABLE_NAME, NUM_ROWS FROM ALL_TABLES WHERE OWNER='SCHEMA';
To get the number of records in all tables of your schema, use:
SELECT SUM(NUM_ROWS) FROM ALL_TABLES WHERE OWNER='SCHEMA';

Resources