SQL One Liners

From bibbleWiki
Jump to navigation Jump to search

Snowflake

List row count for views in a schema

This creates a command you subsequently execute

select listagg(xx, ' union all ')
from (
    select 'select count(*) c, \'' || x || '\' v from ' || x as xx
    from (
        select TABLE_CATALOG ||'.'|| TABLE_SCHEMA ||'."'||TABLE_NAME||'"' x
        from IHC.INFORMATION_SCHEMA.VIEWS
        where table_schema='APPROVALPLUS'
    )
);

Convert date from known format

select to_date('3/4/2013', 'dd/mm/yyyy');

MS SQL

List row count for tables in a schema

SELECT o.NAME,
  i.rowcnt
FROM sysindexes AS i
  INNER JOIN sysobjects AS o ON i.id = o.id
WHERE i.indid < 2  AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0
ORDER BY o.NAME