我见过一些这样的问题 - Count NULL Values from multiple columns with SQL
但是,真的没有办法计算超过 30 列的表中的空值吗?就像我不想按名称指定它们一样?
使用星形宏,除了列名之外,是否还有办法获取列数据类型(布尔值、数值等)?
例如,此查询使用星号宏从引用表中收集列名,将其保存为数组变量column_names,然后循环该数组并将 max 函数应用于所有列。
{% set column_names = star(
from=ref_table,
except=["a", "b", "c"],
as_list=True)
%}
select
date_trunc('week', day) as week,
name,
{%- for col in column_names %}
max({{ col|lower }}) as {{ col | lower }}{%- if not loop.last %},{{ '\n ' }}{% endif %}
{%- endfor %}
from {{ ref('my_table_name') }}
group by 1, 2
Run Code Online (Sandbox Code Playgroud)
我想有条件地将 max 函数仅应用于布尔列。
这可能看起来像
{%- for col in column_names %}
{% if is_boolean(col) %}
max({{ col|lower }}) as {{ col | lower …Run Code Online (Sandbox Code Playgroud)