1

我在雪花中有一张像下面这样的桌子

+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|attrs          |options                                                                                                                                                                                                                                                                                            |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|acct           |[{"key":"spencer","val":"spencer"},{"key":"mart","val":"mart"},{"key":"red-fin","val":"-"}]                                                                                                                                                                                                        |
|fav_activity   |[{"key":"movie","val":"movie"},{"key":"books","val":"books"},{"key":"music","val":"music"},{"key":"swimming","val":"swimming"},{"key":"games","val":"games"},{"key":"team","val":"team"},{"key":"food","val":"food"},{"key":"steam-room","val":"\"steam room\""},{"key":"hiking","val":"hiking"}]  |
+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

现在我需要比较字典列表的每个键和值,并只返回键/值不同的字典的键。

例如,查看 attrs acct- 列表中有 3 个字典。前两个字典的键和值是相同的,但第三个具有不同的键/值。所以我需要返回第三个字典的键,即red-fin

预期输出:

+---------------+------------+
|attrs          |result      |
+---------------+------------+   
|acct           |red-fin     |
|fav_activity   |steam-room  |
+---------------+------------+  

有人可以帮我在雪花中做到这一点。

提前致谢。

4

1 回答 1

2

你只需要展平和比较。

这解决了示例测试用例:

with data as (
select $1 attrs, parse_json($2) options   
from values ('acct', '[{"key":"spencer","val":"spencer"},{"key":"mart","val":"mart"},{"key":"red-fin","val":"-"}]')
, ('fav_activity', '[{"key":"movie","val":"movie"},{"key":"books","val":"books"},{"key":"music","val":"music"},{"key":"swimming","val":"swimming"},{"key":"games","val":"games"},{"key":"team","val":"team"},{"key":"food","val":"food"},{"key":"steam-room","val":"\\"steam room\\""},{"key":"hiking","val":"hiking"}]')
)

select attrs, k.value:key
from data, table(flatten(options)) k
where k.value:key!=k.value:val
;

在此处输入图像描述

于 2021-07-27T04:38:23.287 回答