繁体   English   中英

presto sql 获取表格填充率的查询

[英]presto sql query for getting the fill rate of the table

我想要一个通用查询来获取表中所有列的填充率。无论列号如何,查询都应该工作。我必须使用 presto sql 来实现它。我已经尝试搜索一种方法,但似乎没有任何效果。

输入

一种 C
1个 null null 1个
2个 2个 3个 4个
Null Null Null 5个

Output

一种 C
0.66 0.33 0.33 1.0

解释:A Col 包含 3 行,有 2 个非 null 值所以 2/3 B 和 C Cols 包含 2 个 null 值和一个非 null 值所以 1/3 D col 没有 null 值所以 3/3

提前致谢

AFAIK Presto/Trino 不提供动态查询执行功能(即类似于 T-SQL 中的 EXEC),因此唯一的选择(除非您准备好 go 下用户定义的 function路)编写一个查询,它将枚举所有需要的列(如果您正在使用另一种语言的客户端 - 您可以利用information_schema.columns信息动态构建查询):

with dataset(A, B, C, D) as (
    values (1, null, null, 1),
        (2, 2, 3, 4),
        (Null, Null, Null, 5)
)

select 1.00 * count_if(a is not null) / count(*) a,
    1.00 * count_if(b is not null) / count(*) b,
    1.00 * count_if(c is not null) / count(*) c,
    1.00 * count_if(d is not null) / count(*) d
from dataset;

Output:

一种 b c d
0.67 0.33 0.33 1.00

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM