寻找一个好的解决方案来过滤下面查询中的mapp_agg函数中使用的键。
我正在尝试过滤键列以将映射的大小减小到仅我要创建的列,但是在运行此命令时,我收到键不存在错误。如果我不使用 IN 而是使用 OR,那么它可以工作,但是当我添加其他列时会影响性能
SELECT
id,
metric_id,
kv['uni Amount'] as uni_amt,
kv['mrt Amount'] as mrt_amt
FROM
id,
metric_id,
map_agg(
key,
value
) kv
from table
where key in ('uni Amount', 'mrt Amount')
group by
id,
metric_id)
我也尝试使用map_filter,但无法让它工作
SELECT
id,
metric_id,
kv['uni Amount'] as uni_amt,
kv['mrt Amount'] as mrt_amt
FROM
id,
metric_id,
map_filter(map_agg(
key,
value
), (k,v) k -> k in ('uni Amount', 'mrt Amount')
from table
group by
id,
metric_id)
map_filter
需要具有两个参数(键和值)的函数形式的第二个参数,并导致 bool 可以用匿名/lambda/箭头形式编写可以在很多语言中找到。尝试将 (k,v) k -> k in ('uni Amount', 'mrt Amount')
更改为 (k,v) -> k in ('uni Amount', 'mrt Amount')
(k
和 v
是函数的键和值参数)并添加缺少的括号:
-- ...
map_filter(
map_agg(key, value)
, (k,v) -> k in ('uni Amount', 'mrt Amount'))
-- ...