查询统计雪花 table 的 json 列中没有特定键的所有记录

Query to count all records without certain key in the json column of the snowflake table

我正在尝试从 Snowflake 中获取记录数,而该特定记录的 json 列中没有某些键。

这是雪花 table 的样子:

EMP_ID|DEPARTMENT_NAME|EID|DETAILS
EMP10001 | Finance |10008918 |{                                                                                 
"name": "Alec George",                                                        
"Year_Joined": "2013",
"Ready_to_transfer":  "no",
"Ready_to_permanently_WFH":  "yes",
}

现在我想计算雪花 table 的详细信息列中没有以 Ready_ 开头的键的记录,并按 Department_Name 分组计数。

注意:详情中可以有多个以Ready_开头的key

目前我的计数查询正在返回记录,其中还列出了以 Ready_ 开头的键。

您可以展平以获取所有键,然后对于每条记录,您可以计算以所需字符串开头的键的数量:

with data as (
select  emp_id,  dep,  eid, parse_json() details
from (values
('EMP10001','Finance', 10008918, '{
"name": "Alec George",
"Year_Joined": "2013", "Ready_to_transfer": "no", "Ready_to_permanently_WFH": "yes", }')
,('EMP10002','Finance', 10008918, '{
"name": "Alex George",
"Year_Joined": "2013", }')
)
)

select seq, count_if(detail.key like 'Ready_%') how_many_ready
from data, table(flatten(details)) detail
group by 1
;

那么你只需要计算计数 > 0 的元素的数量。