【问题标题】:Query to count all rows in all Snowflake views查询以计算所有雪花视图中的所有行
【发布时间】:2021-03-16 17:09:01
【问题描述】:

我正在尝试对我的 Snowflake 数据库中一组视图中的所有行进行计数。

不幸的是,information_schema.tables 中的内置 row_count 不存在于 information_schema.views 中。

看来我需要count 每个视图中的所有行,例如:

with view_name as (select table_name 
    from account_usage.views
    where table_schema = 'ACCESS' and RIGHT(table_name,7) = 'CURRENT'
                  )
select count (*) from view_name;

但这只会返回一个结果,而不是每行一个结果

如果我更改 select 以包含视图名称,即

select concat('Rows in ', view_name), count (*) from view_name;

…它返回错误 "invalid identifier 'VIEW_NAME' (line 5)"

如何显示所有结果并包含视图名称?

【问题讨论】:

  • 你调查过 SP 吗? SP 可以遍历information_schema.views,为这些视图运行每个计数,然后将结果插入到表中,然后您可以在 SP 填充信息后查询表。
  • 我做了,但这是一个 DWH 设置,我不想使用任何 SP。另外,我仍然需要编写代码进行迭代,只是格式不同。
  • 目前,我只是为我关心的 10 个左右的表手动完成。仍然对自动化感兴趣。

标签: sql snowflake-cloud-data-platform


【解决方案1】:

您可以创建一个查看 information_schema 的查询来创建一个查询,该查询将逐个查看获取其计数:

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  KNOEMA_FORECAST_DATA_ATLAS.INFORMATION_SCHEMA.VIEWS
        where table_schema='FORECAST'
    )
)

另见How to find the number of rows for all views in a schema?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 2021-09-23
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    相关资源
    最近更新 更多