Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to retrieve the count of columns for a table in SQL and combine it with another SQL query?

I need the count of columns of a table, in below SQL I am getting the desired output, but I also need the count of columns of that ‘PAYMENT’ table.

SELECT COUNT(*) AS TotalRowCount,     
MAX(MODIFIEDUTC) AS newMODIFIEDUTC, 
MIN(MODIFIEDUTC) AS oldMODIFIEDUTC, 
MAX(CREATEDUTC) AS newCREATEDUTC, 
MIN(CREATEDUTC) AS oldCREATEDUTC, 
DATEDIFF(DAY, MAX(MODIFIEDUTC), GETDATE()) AS Freshness 
FROM NAVI.PAYMENT;

How do I get columns count without disturbing above sql query. I need to run this query in SNOWFLAKE and also in Microsoft SQL server management studio.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

A cross-platform query would look like

select count(*)
from INFORMATION_SCHEMA.COLUMNS 
where table_schema = 'NAVI' and table_name = 'PAYMENT';

To incorporate in existing query you would simply do:

SELECT COUNT(*) AS TotalRowCount,     
..., (
  select count(*)
  from INFORMATION_SCHEMA.COLUMNS 
  where table_schema = 'NAVI' and table_name = 'PAYMENT'
) as ColumnCount
FROM NAVI.PAYMENT;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading