Sunday, 11 December 2022

Get the record count from all tables in a schema

select table_schema, 

       table_name, 

       (xpath('/row/cnt/text()', xml_count))[1]::text::int as row_count

from (

  select table_name, table_schema, 

         query_to_xml(format('select count(*) as cnt from %I.%I', table_schema, table_name), false, true, '') as xml_count

  from information_schema.tables

  where table_schema = 'test'

AND table_name IN ('abc') --<< change here for the schema you want

) t order by 3 DESC


---------------------------------------------

APPROACH 2 with WHERE CONDITION

---------------------------------------------


select table_schema, 

       table_name, 

       (xpath('/row/cnt/text()', xml_count))[1]::text::int as row_count

from (

  select table_name, table_schema, 

         query_to_xml(format('select count(*) as cnt from %I.%I  WHERE updated > ''2022-10-01 00:00:00''', table_schema, table_name), false, true, '') as xml_count

  from information_schema.tables

  where table_schema = 'test'

AND table_name NOT IN ('abc')

AND table_name NOT LIKE ('ad_%') --<< change here for the schema you want

) t order by 3 DESC

DBT - Models

Models are where your developers spend most of their time within a dbt environment. Models are primarily written as a select statement and ...