SQL - 在雪花中一次查找多个表的记录数
SQL - Find Record count for multiple tables at a time in snowflake
我想看到计数有 1 次表,而不是每个 运行。
对于 EX:
select COUNT(*) from "Fact_MASTER ";
select COUNT(*) from "Dim_MASTER ";
select COUNT(*) from "Fact2 ";
select COUNT(*) from "Dim2";
select COUNT(*) from "Fact3";
select COUNT(*) from "Dim3"
我们有什么方法可以编写 CTE 来提取临时 table 中每个记录的记录计数,如下所示:
您可以使用 union all
:
select 'Fact_MASTER', COUNT(*) from "Fact_MASTER " union all
select 'Dim_MASTER', COUNT(*) from "Dim_MASTER " union all
select 'Fact2', COUNT(*) from "Fact2 " union all
select 'Dim2', COUNT(*) from "Dim2" union all
select 'Fact3', COUNT(*) from "Fact3" union all
select 'Dim3', COUNT(*) from "Dim3"
您似乎希望每个计数都在单独的列中。如果是这样,您可以将每个查询转换为单独的子查询,并且 select
它们:
select
(select count(*) from "Fact_MASTER") as fact_master,
(select count(*) from "Dim_MASTER ") as dim_master,
(select count(*) from "Fact2") as fact2,
(select count(*) from "Dim2") as dim2,
(select count(*) from "Fact3") as fact3
(select count(*) from "Dim3") as dim3
你试过吗运行:
SHOW TABLES;
如果您随后想将该信息用于其他用途,您可以进行后续操作,例如:
select "rows" as cnt
from table(result_scan(last_query_id()))
where "name" in (...);
如果您有一组要PIVOT
的表格列表,您还可以使用result_scan()
函数对数据进行透视:
https://docs.snowflake.com/en/sql-reference/constructs/pivot.html
我想看到计数有 1 次表,而不是每个 运行。
对于 EX:
select COUNT(*) from "Fact_MASTER ";
select COUNT(*) from "Dim_MASTER ";
select COUNT(*) from "Fact2 ";
select COUNT(*) from "Dim2";
select COUNT(*) from "Fact3";
select COUNT(*) from "Dim3"
我们有什么方法可以编写 CTE 来提取临时 table 中每个记录的记录计数,如下所示:
您可以使用 union all
:
select 'Fact_MASTER', COUNT(*) from "Fact_MASTER " union all
select 'Dim_MASTER', COUNT(*) from "Dim_MASTER " union all
select 'Fact2', COUNT(*) from "Fact2 " union all
select 'Dim2', COUNT(*) from "Dim2" union all
select 'Fact3', COUNT(*) from "Fact3" union all
select 'Dim3', COUNT(*) from "Dim3"
您似乎希望每个计数都在单独的列中。如果是这样,您可以将每个查询转换为单独的子查询,并且 select
它们:
select
(select count(*) from "Fact_MASTER") as fact_master,
(select count(*) from "Dim_MASTER ") as dim_master,
(select count(*) from "Fact2") as fact2,
(select count(*) from "Dim2") as dim2,
(select count(*) from "Fact3") as fact3
(select count(*) from "Dim3") as dim3
你试过吗运行:
SHOW TABLES;
如果您随后想将该信息用于其他用途,您可以进行后续操作,例如:
select "rows" as cnt
from table(result_scan(last_query_id()))
where "name" in (...);
如果您有一组要PIVOT
的表格列表,您还可以使用result_scan()
函数对数据进行透视:
https://docs.snowflake.com/en/sql-reference/constructs/pivot.html