[英]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.